View Javadoc
1   package at.rseiler.spbee.core.pojo;
2   
3   import java.io.Serializable;
4   import java.util.Optional;
5   
6   /**
7    * Holds the type information which consists of the type and the generic type.
8    *
9    * @author Reinhard Seiler {@literal <rseiler.developer@gmail.com>}
10   */
11  public class TypeInfo implements Serializable {
12  
13      private static final long serialVersionUID = -6281028938216215481L;
14  
15      private final String type;
16      private final String genericType;
17  
18      /**
19       * Constructs a new TypeInfo so that it represents a basic type.
20       *
21       * @param type the type
22       */
23      public TypeInfo(String type) {
24          this(type, null);
25      }
26  
27      /**
28       * Constructs a new TypeInfo so that it represents a basic type or a type with generics.
29       *
30       * @param type        the type
31       * @param genericType the generic type
32       */
33      public TypeInfo(String type, String genericType) {
34          this.type = type;
35          this.genericType = genericType;
36      }
37  
38      /**
39       * Returns the base type.
40       * E.g.:
41       * <pre>
42       * String -> String
43       * List<String> -> List
44       * </pre>
45       *
46       * @return the base type
47       */
48      public String getType() {
49          return type;
50      }
51  
52      /**
53       * Returns the generic type.
54       * E.g.:
55       * <pre>
56       * List<String> -> String;
57       * String -> Optional.empty()
58       * </pre>
59       *
60       * @return the generic type
61       */
62      public Optional<String> getGenericType() {
63          return Optional.ofNullable(genericType);
64      }
65  
66      /**
67       * Returns the generic type if it exists otherwise the base type.
68       * E.g.:
69       * <pre>
70       * String -> String
71       * List<String> -> String
72       * </pre>
73       *
74       * @return the generic type or the base type
75       */
76      public String getGenericTypeOrType() {
77          return Optional.ofNullable(genericType).orElse(type);
78      }
79  
80      /**
81       * Returns the string representation of the type and generic type.
82       *
83       * @return the string representation
84       */
85      public String asString() {
86          return genericType != null ? type + '<' + genericType + '>' : type;
87      }
88  
89      @Override
90      public String toString() {
91          return "TypeInfo{" +
92                  "type='" + type + '\'' +
93                  ", genericType=" + genericType +
94                  '}';
95      }
96  
97  }