View Javadoc

1   /*
2    * Copyright 2008-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.entropysoft.transmorph.signature;
17  
18  /**
19   * Signature for an array type
20   *
21   * This class is thread-safe
22   * 
23   * @author Cedric Chabanois (cchabanois at gmail.com)
24   *
25   */
26  public class ArrayTypeSignature extends FieldTypeSignature {
27  
28  	private final FullTypeSignature componentTypeSignature;
29  	private volatile FullTypeSignature typeErasureSignature;
30  
31  	public ArrayTypeSignature(FullTypeSignature componentTypeSignature) {
32  		this.componentTypeSignature = componentTypeSignature;
33  	}
34  
35  	/**
36  	 * get the component TypeSignature
37  	 * 
38  	 * @return
39  	 */
40  	public FullTypeSignature getComponentTypeSignature() {
41  		return componentTypeSignature;
42  	}
43  
44  	/**
45  	 * get the element typeSignature
46  	 * 
47  	 * @return
48  	 */
49  	public FullTypeSignature getElementTypeSignature() {
50  		if (getComponentTypeSignature().isArrayType()) {
51  			return ((ArrayTypeSignature) getComponentTypeSignature())
52  					.getElementTypeSignature();
53  		} else {
54  			return getComponentTypeSignature();
55  		}
56  	}
57  
58  	/**
59  	 * get the number of dimensions
60  	 * 
61  	 * @return
62  	 */
63  	public int getNumDimensions() {
64  		if (getComponentTypeSignature().isArrayType()) {
65  			return ((ArrayTypeSignature) getComponentTypeSignature())
66  					.getNumDimensions() + 1;
67  		} else {
68  			return 1;
69  		}
70  	}
71  
72  	public boolean isArrayType() {
73  		return true;
74  	}
75  
76  	@Override
77  	public String toString() {
78  		return getSignature();
79  	}
80  
81  	@Override
82  	public FullTypeSignature getTypeErasureSignature() {
83  		if (typeErasureSignature == null) {
84  			typeErasureSignature = new ArrayTypeSignature(
85  					componentTypeSignature.getTypeErasureSignature());
86  		}
87  		return typeErasureSignature;
88  	}
89  
90  }