java/dictionary-generator/src/cz/frantovo/telco/dictionary/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 12 Aug 2013 18:12:50 +0200
changeset 40 6c34689e894a
parent 15 93208f791318
child 47 b87ea854d920
permissions -rw-r--r--
data: MUA
     1 /**
     2  * Free Telco Dictionary
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.telco.dictionary;
    19 
    20 import java.io.Closeable;
    21 import java.io.IOException;
    22 import java.util.HashSet;
    23 import java.util.Iterator;
    24 import java.util.Map;
    25 import java.util.Map.Entry;
    26 import java.util.Set;
    27 import java.util.logging.Level;
    28 import java.util.logging.Logger;
    29 import org.w3c.dom.Node;
    30 import org.w3c.dom.NodeList;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class Functions {
    37 
    38 	private static final Logger log = Logger.getLogger(Functions.class.getName());
    39 
    40 	public static Iterable<Node> nodeIterable(final NodeList list) {
    41 		return new Iterable<Node>() {
    42 			@Override
    43 			public Iterator<Node> iterator() {
    44 				return new Iterator<Node>() {
    45 					int position;
    46 
    47 					@Override
    48 					public boolean hasNext() {
    49 						return position < list.getLength();
    50 					}
    51 
    52 					@Override
    53 					public Node next() {
    54 						return list.item(position++);
    55 					}
    56 
    57 					@Override
    58 					public void remove() {
    59 						throw new UnsupportedOperationException("remove not supported");
    60 					}
    61 				};
    62 			}
    63 		};
    64 	}
    65 
    66 	public static void close(Closeable c) {
    67 		try {
    68 			c.close();
    69 		} catch (Exception e) {
    70 			log.log(Level.WARNING, "closing of " + c + " has failed", e);
    71 		}
    72 	}
    73 
    74 	public static boolean equalz(Object a, Object b) {
    75 		return a == null ? b == null : a.equals(b);
    76 	}
    77 
    78 	public static <K, V> Set<K> getKeysForValue(Map<K, V> map, V value) {
    79 		Set<K> keysFound = new HashSet<>();
    80 
    81 		for (Entry<K, V> entry : map.entrySet()) {
    82 			if (equalz(value, entry.getValue())) {
    83 				keysFound.add(entry.getKey());
    84 			}
    85 		}
    86 
    87 		return keysFound;
    88 	}
    89 
    90 	private Functions() {
    91 	}
    92 }