java/dictionary-generator/src/cz/frantovo/telco/dictionary/Functions.java
changeset 15 93208f791318
child 47 b87ea854d920
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/dictionary-generator/src/cz/frantovo/telco/dictionary/Functions.java	Tue Jul 09 18:59:07 2013 +0200
     1.3 @@ -0,0 +1,92 @@
     1.4 +/**
     1.5 + * Free Telco Dictionary
     1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.telco.dictionary;
    1.22 +
    1.23 +import java.io.Closeable;
    1.24 +import java.io.IOException;
    1.25 +import java.util.HashSet;
    1.26 +import java.util.Iterator;
    1.27 +import java.util.Map;
    1.28 +import java.util.Map.Entry;
    1.29 +import java.util.Set;
    1.30 +import java.util.logging.Level;
    1.31 +import java.util.logging.Logger;
    1.32 +import org.w3c.dom.Node;
    1.33 +import org.w3c.dom.NodeList;
    1.34 +
    1.35 +/**
    1.36 + *
    1.37 + * @author Ing. František Kučera (frantovo.cz)
    1.38 + */
    1.39 +public class Functions {
    1.40 +
    1.41 +	private static final Logger log = Logger.getLogger(Functions.class.getName());
    1.42 +
    1.43 +	public static Iterable<Node> nodeIterable(final NodeList list) {
    1.44 +		return new Iterable<Node>() {
    1.45 +			@Override
    1.46 +			public Iterator<Node> iterator() {
    1.47 +				return new Iterator<Node>() {
    1.48 +					int position;
    1.49 +
    1.50 +					@Override
    1.51 +					public boolean hasNext() {
    1.52 +						return position < list.getLength();
    1.53 +					}
    1.54 +
    1.55 +					@Override
    1.56 +					public Node next() {
    1.57 +						return list.item(position++);
    1.58 +					}
    1.59 +
    1.60 +					@Override
    1.61 +					public void remove() {
    1.62 +						throw new UnsupportedOperationException("remove not supported");
    1.63 +					}
    1.64 +				};
    1.65 +			}
    1.66 +		};
    1.67 +	}
    1.68 +
    1.69 +	public static void close(Closeable c) {
    1.70 +		try {
    1.71 +			c.close();
    1.72 +		} catch (Exception e) {
    1.73 +			log.log(Level.WARNING, "closing of " + c + " has failed", e);
    1.74 +		}
    1.75 +	}
    1.76 +
    1.77 +	public static boolean equalz(Object a, Object b) {
    1.78 +		return a == null ? b == null : a.equals(b);
    1.79 +	}
    1.80 +
    1.81 +	public static <K, V> Set<K> getKeysForValue(Map<K, V> map, V value) {
    1.82 +		Set<K> keysFound = new HashSet<>();
    1.83 +
    1.84 +		for (Entry<K, V> entry : map.entrySet()) {
    1.85 +			if (equalz(value, entry.getValue())) {
    1.86 +				keysFound.add(entry.getKey());
    1.87 +			}
    1.88 +		}
    1.89 +
    1.90 +		return keysFound;
    1.91 +	}
    1.92 +
    1.93 +	private Functions() {
    1.94 +	}
    1.95 +}