franta-hg@15: /** franta-hg@15: * Free Telco Dictionary franta-hg@15: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@15: * franta-hg@15: * This program is free software: you can redistribute it and/or modify franta-hg@15: * it under the terms of the GNU General Public License as published by franta-hg@151: * the Free Software Foundation, version 3 of the License. franta-hg@15: * franta-hg@15: * This program is distributed in the hope that it will be useful, franta-hg@15: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@15: * GNU General Public License for more details. franta-hg@15: * franta-hg@15: * You should have received a copy of the GNU General Public License franta-hg@15: * along with this program. If not, see . franta-hg@15: */ franta-hg@15: package cz.frantovo.telco.dictionary; franta-hg@15: franta-hg@15: import java.io.Closeable; franta-hg@15: import java.util.HashSet; franta-hg@15: import java.util.Iterator; franta-hg@15: import java.util.Map; franta-hg@15: import java.util.Map.Entry; franta-hg@15: import java.util.Set; franta-hg@15: import java.util.logging.Level; franta-hg@15: import java.util.logging.Logger; franta-hg@15: import org.w3c.dom.Node; franta-hg@15: import org.w3c.dom.NodeList; franta-hg@15: franta-hg@15: /** franta-hg@130: * Common functions franta-hg@130: * franta-hg@15: * @author Ing. František Kučera (frantovo.cz) franta-hg@15: */ franta-hg@15: public class Functions { franta-hg@15: franta-hg@15: private static final Logger log = Logger.getLogger(Functions.class.getName()); franta-hg@15: franta-hg@15: public static Iterable nodeIterable(final NodeList list) { franta-hg@15: return new Iterable() { franta-hg@15: @Override franta-hg@15: public Iterator iterator() { franta-hg@15: return new Iterator() { franta-hg@15: int position; franta-hg@15: franta-hg@15: @Override franta-hg@15: public boolean hasNext() { franta-hg@15: return position < list.getLength(); franta-hg@15: } franta-hg@15: franta-hg@15: @Override franta-hg@15: public Node next() { franta-hg@15: return list.item(position++); franta-hg@15: } franta-hg@15: franta-hg@15: @Override franta-hg@15: public void remove() { franta-hg@15: throw new UnsupportedOperationException("remove not supported"); franta-hg@15: } franta-hg@15: }; franta-hg@15: } franta-hg@15: }; franta-hg@15: } franta-hg@15: franta-hg@15: public static void close(Closeable c) { franta-hg@15: try { franta-hg@15: c.close(); franta-hg@15: } catch (Exception e) { franta-hg@15: log.log(Level.WARNING, "closing of " + c + " has failed", e); franta-hg@15: } franta-hg@15: } franta-hg@15: franta-hg@15: public static boolean equalz(Object a, Object b) { franta-hg@15: return a == null ? b == null : a.equals(b); franta-hg@15: } franta-hg@15: franta-hg@15: public static Set getKeysForValue(Map map, V value) { franta-hg@15: Set keysFound = new HashSet<>(); franta-hg@15: franta-hg@15: for (Entry entry : map.entrySet()) { franta-hg@15: if (equalz(value, entry.getValue())) { franta-hg@15: keysFound.add(entry.getKey()); franta-hg@15: } franta-hg@15: } franta-hg@15: franta-hg@15: return keysFound; franta-hg@15: } franta-hg@15: franta-hg@15: private Functions() { franta-hg@15: } franta-hg@15: }