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