View Javadoc

1   /*
2    * VectorGraphics2D: Vector export for Java(R) Graphics2D
3    *
4    * (C) Copyright 2010 Erich Seifert <dev[at]erichseifert.de>
5    *
6    * This file is part of VectorGraphics2D.
7    *
8    * VectorGraphics2D is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU Lesser General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or
11   * (at your option) any later version.
12   *
13   * VectorGraphics2D is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with VectorGraphics2D.  If not, see <http://www.gnu.org/licenses/>.
20   */
21  
22  package de.erichseifert.vectorgraphics2d;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * Abstract class that contains utility functions for working with data
29   * collections like maps or lists.
30   */
31  public abstract class DataUtils {
32  	/**
33  	 * Creates a mapping from two arrays, one with keys, one with values.
34  	 * @param <K> Data type of the keys.
35  	 * @param <V> Data type of the values.
36  	 * @param keys Array containing the keys.
37  	 * @param values Array containing the values.
38  	 * @return Map with keys and values from the specified arrays.
39  	 */
40  	public static <K,V> Map<K, V> map(K[] keys, V[] values) {
41  		// Check for valid parameters
42  		if (keys.length != values.length) {
43  			throw new IllegalArgumentException("Number of keys and values is different. Cannot create map.");
44  		}
45  		// Fill map with keys and values
46  		Map<K, V> map = new HashMap<K, V>();
47  		for (int i = 0; i < keys.length; i++) {
48  			K key = keys[i];
49  			V value = values[i];
50  			map.put(key, value);
51  		}
52  		return map;
53  	}
54  
55  	/**
56  	 * Returns a string with all float values divided by a specified separator.
57  	 * @param separator Separator string.
58  	 * @param elements Float array.
59  	 * @return Joined string.
60  	 */
61  	public static String join(String separator, float... elements) {
62  		if (elements == null || elements.length == 0) {
63  			return "";
64  		}
65  		StringBuffer sb = new StringBuffer(elements.length*3);
66  		for (int i = 0; i < elements.length; i++) {
67  			if (i > 0) {
68  				sb.append(separator);
69  			}
70  			sb.append(elements[i]);
71  		}
72  		return sb.toString();
73  	}
74  
75  	/**
76  	 * Returns a string with all float values divided by a specified separator.
77  	 * @param separator Separator string.
78  	 * @param elements Double array.
79  	 * @return Joined string.
80  	 */
81  	public static String join(String separator, double... elements) {
82  		if (elements == null || elements.length == 0) {
83  			return "";
84  		}
85  		StringBuffer sb = new StringBuffer(elements.length*3);
86  		for (int i = 0; i < elements.length; i++) {
87  			if (i > 0) {
88  				sb.append(separator);
89  			}
90  			sb.append(elements[i]);
91  		}
92  		return sb.toString();
93  	}
94  }