View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
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 Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.ObjectOutputStream;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  import org.apache.commons.codec.binary.Hex;
28  
29  /**
30   *
31   * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
32   */
33  public class PdfmuUtils {
34  
35      /**
36       * Converts an array of keys and an array of values to a {@link Map}.
37       *
38       * @param <K> the type of keys.
39       * @param <V> the type of values.
40       * @param keys the array of keys. Only the keys that have a matching value
41       * are used.
42       * @param values the array of values. Only the values that have a matching
43       * key are used.
44       * @return a {@link Map} that contains a key-value pair for each index in
45       * keys and values. The order of the entries is preserved by the underlying
46       * implementation ({@link LinkedHashMap}).
47       */
48      // TODO: Rename
49      public static <K, V> Map<K, V> sortedMap(K[] keys, V[] values) {
50          Map<K, V> result = new LinkedHashMap<>();
51          int n = Math.min(keys.length, values.length);
52          for (int i = 0; i < n; ++i) {
53              result.put(keys[i], values[i]);
54          }
55          return result;
56      }
57  
58      // TODO: Rename
59      public static <K, V> Map<K, V> sortedMap(Map.Entry<K, V>... entries) {
60          Map<K, V> result = new LinkedHashMap<>();
61          for (Map.Entry<K, V> entry : entries) {
62              K key = entry.getKey();
63              V value = entry.getValue();
64              result.put(key, value);
65          }
66          return result;
67      }
68  
69      /**
70       * Extracts named groups from a matcher.
71       *
72       * @param m the matcher.
73       * @param names the names of the groups.
74       * @return a sorted map that assigns group values to the names.
75       */
76      public static Map<String, String> getMatcherGroups(Matcher m, List<String> names) {
77          assert m != null;
78          assert names != null;
79          Map<String, String> result = new LinkedHashMap<>();
80          for (String name : names) {
81              result.put(name, m.group(name));
82          }
83          return result;
84      }
85  }