1 package org.unicode.cldr.icu; 2 3 import java.util.Collection; 4 import java.util.HashSet; 5 import java.util.Iterator; 6 import java.util.Set; 7 8 /** 9 * Superclass for mappers that convert CLDR data to ICU text files. 10 * @author jchye 11 */ 12 public abstract class Mapper { 13 // Store list of sources for makefile generation. 14 protected Set<String> sources = new HashSet<String>(); 15 16 /** 17 * Converts the specified locale into one or more IcuData objects. 18 */ fillFromCldr(String locale)19 public abstract IcuData[] fillFromCldr(String locale); // private/protected? 20 21 /** 22 * Returns an iterator that iterates through all of the CLDR data within the 23 * scope of this mapper and matching the specified filter and returns the generated IcuData objects. 24 */ iterator(final Filter filter)25 public Iterator<IcuData> iterator(final Filter filter) { 26 IcuDataIterator iterator = new IcuDataIterator(filter); 27 iterator.init(); 28 return iterator; 29 } 30 31 private class IcuDataIterator implements Iterator<IcuData> { 32 private Filter filter; 33 private Iterator<String> localeIterator = getAvailable().iterator(); 34 private IcuData[] curArray; 35 private int curIndex = -1; 36 IcuDataIterator(Filter filter)37 private IcuDataIterator(Filter filter) { 38 this.filter = filter; 39 } 40 41 /** 42 * Initializes internal variables before starting iteration. 43 */ init()44 private void init() { 45 curArray = new IcuData[0]; 46 loadNextPos(); 47 } 48 49 @Override hasNext()50 public boolean hasNext() { 51 return localeIterator.hasNext() || curIndex < curArray.length; 52 } 53 54 @Override next()55 public IcuData next() { 56 IcuData icuData = curArray[curIndex]; 57 sources.add(icuData.getName()); 58 // Move to next valid position. 59 loadNextPos(); 60 return icuData; 61 } 62 63 /** 64 * Prepares the next item in the iterator. 65 */ loadNextPos()66 private void loadNextPos() { 67 do { 68 curIndex++; 69 if (curIndex == curArray.length) { 70 String locale = null; 71 do { 72 if (!localeIterator.hasNext()) { 73 // No more items left in the iterator. 74 return; 75 } 76 locale = localeIterator.next(); 77 } while (!filter.includes(locale)); 78 curArray = fillFromCldr(locale); 79 curIndex = 0; 80 } 81 } while (!filter.includes(curArray[curIndex].getName())); 82 } 83 84 @Override remove()85 public void remove() { 86 throw new UnsupportedOperationException(); 87 } 88 } 89 90 /** 91 * @return the set of IcuData objects that were generated by the mapper. 92 */ getGenerated()93 public Set<String> getGenerated() { 94 return sources; 95 } 96 97 /** 98 * Returns the set of all CLDR locales available for conversion. 99 */ getAvailable()100 public abstract Collection<String> getAvailable(); 101 102 /** 103 * Generates a makefile from the list of locales converted by this mapper. 104 * @param aliases any aliases that were created apart from this mapper 105 * @return the generated makefile 106 */ generateMakefile(Collection<String> aliases)107 public abstract Makefile generateMakefile(Collection<String> aliases); 108 } 109