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