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