001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu; 018 019import java.io.ByteArrayOutputStream; 020import java.io.IOException; 021import java.io.ObjectOutputStream; 022import java.util.LinkedHashMap; 023import java.util.List; 024import java.util.Map; 025import java.util.regex.Matcher; 026import java.util.regex.Pattern; 027import org.apache.commons.codec.binary.Hex; 028 029/** 030 * 031 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a> 032 */ 033public class PdfmuUtils { 034 035 /** 036 * Converts an array of keys and an array of values to a {@link Map}. 037 * 038 * @param <K> the type of keys. 039 * @param <V> the type of values. 040 * @param keys the array of keys. Only the keys that have a matching value 041 * are used. 042 * @param values the array of values. Only the values that have a matching 043 * key are used. 044 * @return a {@link Map} that contains a key-value pair for each index in 045 * keys and values. The order of the entries is preserved by the underlying 046 * implementation ({@link LinkedHashMap}). 047 */ 048 // TODO: Rename 049 public static <K, V> Map<K, V> sortedMap(K[] keys, V[] values) { 050 Map<K, V> result = new LinkedHashMap<>(); 051 int n = Math.min(keys.length, values.length); 052 for (int i = 0; i < n; ++i) { 053 result.put(keys[i], values[i]); 054 } 055 return result; 056 } 057 058 // TODO: Rename 059 public static <K, V> Map<K, V> sortedMap(Map.Entry<K, V>... entries) { 060 Map<K, V> result = new LinkedHashMap<>(); 061 for (Map.Entry<K, V> entry : entries) { 062 K key = entry.getKey(); 063 V value = entry.getValue(); 064 result.put(key, value); 065 } 066 return result; 067 } 068 069 /** 070 * Extracts named groups from a matcher. 071 * 072 * @param m the matcher. 073 * @param names the names of the groups. 074 * @return a sorted map that assigns group values to the names. 075 */ 076 public static Map<String, String> getMatcherGroups(Matcher m, List<String> names) { 077 assert m != null; 078 assert names != null; 079 Map<String, String> result = new LinkedHashMap<>(); 080 for (String name : names) { 081 result.put(name, m.group(name)); 082 } 083 return result; 084 } 085}