source: osm/applications/editors/josm/plugins/opendata/includes/org/geotools/util/CanonicalSet.java@ 28000

Last change on this file since 28000 was 28000, checked in by donvip, 12 years ago

Import new "opendata" JOSM plugin

File size: 3.7 KB
Line 
1/*
2 * GeoTools - The Open Source Java GIS Toolkit
3 * http://geotools.org
4 *
5 * (C) 2001-2008, Open Source Geospatial Foundation (OSGeo)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation;
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17package org.geotools.util;
18
19
20/**
21 * A canonical set of objects, used to optimize memory use.
22 * The operation of this set is similar in spirit to the {@link String#intern} method.
23 * The following example shows a convenient way to use {@code CanonicalSet} as an
24 * internal pool of immutable objects.
25 *
26 * <blockquote><pre>
27 * public Foo create(String definition) {
28 * Foo created = new Foo(definition);
29 * return (Foo) canonicalSet.unique(created);
30 * }
31 * </pre></blockquote>
32 *
33 * The {@code CanonicalSet} has a {@link #get} method that is not part of the {@link java.util.Set}
34 * interface. This {@code get} method retrieves an entry from this set that is equals to
35 * the supplied object. The {@link #unique} method combines a {@code get} followed by a
36 * {@code put} operation if the specified object was not in the set.
37 * <p>
38 * The set of objects is held by weak references as explained in {@link WeakHashSet}.
39 * The {@code CanonicalSet} class is thread-safe.
40 *
41 * @param <E> The type of elements in the set.
42 *
43 * @since 2.4
44 *
45 * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/modules/library/metadata/src/main/java/org/geotools/util/CanonicalSet.java $
46 * @version $Id: CanonicalSet.java 37298 2011-05-25 05:16:15Z mbedward $
47 * @author Martin Desruisseaux (IRD)
48 * @author Jody Garnett
49 */
50public class CanonicalSet<E> extends WeakHashSet<E> {
51 /**
52 * Constructs a {@code CanonicalSet}.
53 *
54 * @deprecated Use {@link #newInstance} instead.
55 */
56 public CanonicalSet() {
57 }
58
59 /**
60 * Constructs a {@code CanonicalSet} for elements of the specified type.
61 *
62 * @param type The type of elements in the set.
63 *
64 * @since 2.5
65 */
66 protected CanonicalSet(final Class<E> type) {
67 super(type);
68 }
69
70 /**
71 * Constructs a {@code CanonicalSet} for elements of the specified type.
72 *
73 * @param <E> The type of elements in the set.
74 * @param type The type of elements in the set.
75 * @return An initially empty set for elements of the given type.
76 *
77 * @since 2.5
78 */
79 public static <E> CanonicalSet<E> newInstance(final Class<E> type) {
80 return new CanonicalSet<E>(type);
81 }
82
83 /**
84 * Returns an object equals to {@code object} if such an object already exist in this
85 * {@code CanonicalSet}. Otherwise, adds {@code object} to this {@code CanonicalSet}.
86 * This method is equivalents to the following code:
87 *
88 * <blockquote><pre>
89 * if (object != null) {
90 * Object current = get(object);
91 * if (current != null) {
92 * return current;
93 * } else {
94 * add(object);
95 * }
96 * }
97 * return object;
98 * </pre></blockquote>
99 *
100 * @param <T> The type of the element to get.
101 * @param object The element to get or to add in the set if not already presents.
102 * @return An element equals to the given one if already presents in the set,
103 * or the given {@code object} otherwise.
104 */
105 public synchronized <T extends E> T unique(final T object) {
106 return intern(object, INTERN);
107 }
108}
Note: See TracBrowser for help on using the repository browser.