PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/appengine/datanucleus/Utils.java

http://datanucleus-appengine.googlecode.com/
Java | 90 lines | 54 code | 14 blank | 22 comment | 1 complexity | ed8f14dd448940dc324c6c4219394136 MD5 | raw file
 1/**********************************************************************
 2Copyright (c) 2009 Google Inc.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15**********************************************************************/
16package com.google.appengine.datanucleus;
17
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.Collection;
21import java.util.HashMap;
22import java.util.HashSet;
23import java.util.LinkedHashSet;
24import java.util.LinkedList;
25import java.util.List;
26import java.util.TreeSet;
27
28/**
29 * A handful of utilities based on the Google Collections Library.
30 * Ultimately I'd like to just bundle this with the ORM but at the moment I
31 * don't want to introduce any more dependencies than absolutely necessary.
32 *
33 * @author Max Ross <maxr@google.com>
34 */
35public class Utils {
36
37  private Utils() {}
38
39  public static <T> ArrayList<T> newArrayList(T... elements) {
40    return new ArrayList<T>(Arrays.asList(elements));
41  }
42
43  public static <T> LinkedList<T> newLinkedList(T... elements) {
44    return new LinkedList<T>(Arrays.asList(elements));
45  }
46
47  public static <T> HashSet<T> newHashSet(T... elements) {
48    return new HashSet<T>(Arrays.asList(elements));
49  }
50
51  public static <T> HashSet<T> newHashSet(Collection<T> elements) {
52    return new HashSet<T>(elements);
53  }
54
55  public static <T> TreeSet<T> newTreeSet(T... elements) {
56    return new TreeSet<T>(Arrays.asList(elements));
57  }
58
59  public static <F, T> List<T> transform(Iterable<F> from, Function<? super F, ? extends T> func) {
60    List<T> to = new ArrayList<T>();
61    for (F fromElement : from) {
62      to.add(func.apply(fromElement));
63    }
64    return to;
65  }
66
67  public static <T> Function<T, T> identity() {
68    return new Function<T, T>() {
69      public T apply(T from) {
70        return from;
71      }
72    };
73  }
74
75  public static <K, V> HashMap<K, V> newHashMap() {
76    return new HashMap<K, V>();
77  }
78
79  public static <T> LinkedHashSet<T> newLinkedHashSet(T... elements) {
80    return new LinkedHashSet<T>(Arrays.asList(elements));
81  }
82
83  public interface Function<F, T> {
84    T apply(F from);
85  }
86
87  public interface Supplier<T> {
88    T get();
89  }
90}