View Javadoc
1   /*
2    * Copyright © 2017, Salesforce.com, Inc
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *     * Redistributions of source code must retain the above copyright
8    *       notice, this list of conditions and the following disclaimer.
9    *     * Redistributions in binary form must reproduce the above copyright
10   *       notice, this list of conditions and the following disclaimer in the
11   *       documentation and/or other materials provided with the distribution.
12   *     * Neither the name of the <organization> nor the
13   *       names of its contributors may be used to endorse or promote products
14   *       derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package com.salesforce.apt.graph.types.impl;
28  
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Iterator;
32  import java.util.LinkedList;
33  import java.util.List;
34  import java.util.Queue;
35  import java.util.stream.Collectors;
36  
37  public class ParseType {
38  
39    private final String type;
40    private final List<ParseType> parameters = new ArrayList<>();
41  
42    public ParseType(String type) {
43      this.type = type;
44    }
45    
46    public ParseType(String type, ParseType... parameters) {
47      this.type = type;
48      this.parameters.addAll(Arrays.asList(parameters));
49    }
50    
51    public String getType() {
52      return type;
53    }
54  
55    public List<ParseType> getParameters() {
56      return parameters;
57    }
58    
59    public static ParseType parse(String type) {
60      final String parseToken = "*";
61      String[] tokens = type
62          .replaceAll("<", parseToken + "<" + parseToken)
63          .replaceAll(">", parseToken + ">" + parseToken)
64          .replaceAll(",", parseToken + "," + parseToken).split("\\" + parseToken);
65      Queue<String> tokenQueue = new LinkedList<>(Arrays.asList(tokens).stream().map(t -> t.trim()).collect(Collectors.toList())); 
66      return parseParameters(tokenQueue);
67    }
68  
69    @Override
70    public int hashCode() {
71      final int prime = 31;
72      int result = 1;
73      result = prime * result + parameters.hashCode();
74      result = prime * result + ((type == null) ? 0 : type.hashCode());
75      return result;
76    }
77  
78    @Override
79    public boolean equals(Object obj) {
80      if (this == obj) {
81        return true;
82      }
83      if (obj == null) {
84        return false;
85      }
86      if (getClass() != obj.getClass()) {
87        return false;
88      }
89      ParseType/../../../../../com/salesforce/apt/graph/types/impl/ParseType.html#ParseType">ParseType other = (ParseType)obj;
90      if (!parameters.equals(other.parameters)) {
91        return false;
92      }
93      if (type == null) {
94        if (other.type != null) {
95          return false;
96        }
97      } else {
98        if (!type.equals(other.type)) {
99          return false;
100       }
101     }
102     return true;
103   }
104 
105   @Override
106   public String toString() {
107     StringBuilder builder = new StringBuilder(getType());
108     if (!getParameters().isEmpty()) {
109       builder.append('<');
110       Iterator<ParseType> iter = getParameters().iterator();
111       while (iter.hasNext()) {
112         builder.append(iter.next().toString());
113         if (iter.hasNext()) {
114           builder.append(',');
115         }
116       }
117       builder.append('>');
118     }
119     return builder.toString();
120   }
121 
122   private static ParseType parseParameters(Queue<String> tokens) {
123     ParseType/types/impl/ParseType.html#ParseType">ParseType child = new ParseType(tokens.remove());
124     if (tokens.isEmpty()) {
125       return child;
126     }
127     if (",".equals(tokens.peek())) {
128       return child;
129     }
130     if ("<".equals(tokens.peek())) {
131       tokens.remove();
132       child.parameters.add(parseParameters(tokens));
133       while (",".equals(tokens.peek())) {
134         tokens.remove();
135         child.parameters.add(parseParameters(tokens));
136       }
137       if (">".equals(tokens.peek())) {
138         tokens.remove();
139       }
140     }
141     return child;
142   }
143     
144 }