View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.util;
21  
22  import java.lang.reflect.ParameterizedType;
23  import java.lang.reflect.Type;
24  import java.util.Arrays;
25  
26  class ParameterizedTypeImpl implements ParameterizedType {
27      private final Class<?> rawType;
28      private final Type[] arguments;
29  
30      @SuppressWarnings("PMD.ArrayIsStoredDirectly")
31      ParameterizedTypeImpl(Class<?> rawType, Type[] arguments) {
32          this.rawType = rawType;
33          this.arguments = arguments;
34      }
35      
36      @Override
37      public Type[] getActualTypeArguments() {
38          // defensive copy
39          return Arrays.copyOf(arguments, arguments.length);
40      }
41  
42      @Override
43      public Type getRawType() {
44          return rawType;
45      }
46  
47      @Override
48      public Type getOwnerType() {
49          return null;
50      }
51      
52      @Override
53      public boolean equals(Object o) {
54          if (!(o instanceof ParameterizedType)) {
55              return false;
56          }
57          ParameterizedType t = (ParameterizedType) o;
58          return t.getRawType().equals(rawType) && 
59                 t.getOwnerType() == null &&
60                 Arrays.equals(arguments, t.getActualTypeArguments());
61      }
62      
63      @Override
64      public int hashCode() {
65          return rawType.hashCode() ^ Arrays.hashCode(arguments);
66      }
67  }