]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/type/StringType.java
Added addFirst/After/Before + remove SCL functions for Ordered Sets
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / StringType.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.type;
13
14 import java.util.Set;
15 import java.util.regex.Pattern;
16 import java.util.regex.PatternSyntaxException;
17
18 import org.simantics.databoard.accessor.error.ReferenceException;
19 import org.simantics.databoard.accessor.reference.ChildReference;
20 import org.simantics.databoard.util.IdentityPair;
21 import org.simantics.databoard.util.Limit;
22 import org.simantics.databoard.util.Range;
23 import org.simantics.databoard.util.RangeException;
24
25 public class StringType extends Datatype {
26                 
27         public static final String KEY_PATTERN = "pattern";
28         public static final String KEY_MIMETYPE = "mimeType";
29         public static final String KEY_LENGTH = "length";
30         
31     private transient Range _length;
32     private transient String _lengthIsForStr;    
33     private transient Pattern pattern_;
34     private transient String pattern_IsForStr;
35     
36     public StringType () {}
37     
38     public StringType (String pattern) {
39         setPattern(pattern);
40     }
41
42     public StringType (String pattern, String mimeType, Range length) {
43         setPattern(pattern);
44         setMimeType(mimeType);
45         setLength(length);
46     }
47
48     public StringType (String pattern, String mimeType, String length) {
49         setPattern(pattern);
50         setMimeType(mimeType);
51         setLength(length);
52     }
53     
54     @Override
55     public int getComponentCount() {
56         return 0;
57     }
58     
59     @Override
60     public Datatype getComponentType(int index) {
61         throw new IllegalArgumentException();
62     }
63     
64     @Override
65     public Datatype getComponentType(ChildReference path) {
66         if (path==null) return this;
67         throw new IllegalArgumentException();
68     }    
69     
70     @Override
71     protected boolean deepEquals(Object obj, Set<IdentityPair<Datatype, Datatype>> compareHistory) {
72                 if (this==obj) return true;
73                 if ( !hasEqualMetadata(obj) ) return false;
74                 return obj instanceof StringType;
75         }
76     
77         @Override
78         public int hashCode() {         
79                 return 0x43676323 + super.hashCode(); 
80         }    
81
82         @Override
83         public void accept(Visitor1 v, Object obj) {
84             v.visit(this, obj);        
85         }
86
87         @Override
88         public <T> T accept(Visitor<T> v) {
89             return v.visit(this);
90         }
91
92         public String getPattern() {
93                 return metadata.get(KEY_PATTERN);
94         }
95
96         public void setPattern(String pattern) 
97         throws PatternSyntaxException 
98         {
99                 if ( pattern == null ) metadata.remove(KEY_PATTERN); else
100                 metadata.put(KEY_PATTERN, pattern);
101         }
102
103         public String getMimeType() {
104                 return metadata.get(KEY_MIMETYPE);
105         }
106
107         public void setMimeType(String mimeType) {
108                 if (mimeType==null) metadata.remove( KEY_MIMETYPE ); else
109                 metadata.put(KEY_MIMETYPE, mimeType);
110         }
111
112         public Range getLength() {
113                 String lengthStr = metadata.get( KEY_LENGTH );
114                 if ( lengthStr == null ) return null;
115                 if ( lengthStr == _lengthIsForStr ) return _length;
116                 try {
117                         _lengthIsForStr = lengthStr;
118                         _length = Range.valueOf( lengthStr );
119                 } catch (RangeException e) {
120                         _length = null;
121                 }
122                 return _length;
123         }
124
125         public int minLength() {
126         if (_length==null) return 0;
127         Limit l = _length.getLower();
128         int value = l.getValue().intValue();
129         if (l.isExclusive()) value++;
130         return value;
131         }
132         
133         public int maxLength() {
134         if (_length==null) return Integer.MAX_VALUE;
135         Limit l = _length.getUpper();
136         int value = l.getValue().intValue();
137         if (l.isExclusive()) value--;
138         return value;
139         }
140
141         /**
142          * Get compiled reg exp pattern 
143          * 
144          * @return pattern or null
145          */
146         public Pattern getCompiledPattern()
147         {
148                 String patternStr = metadata.get( KEY_PATTERN );
149                 if ( patternStr == null ) return null;
150                 if ( patternStr == pattern_IsForStr ) return pattern_;
151                 try {
152                         pattern_IsForStr = patternStr;
153                         pattern_ = Pattern.compile(patternStr);
154                 } catch (PatternSyntaxException e) {
155                         pattern_ = null;
156                 }
157                 return pattern_;
158         }
159
160         public void setLength(String length) {
161                 if ( length == null ) metadata.remove( KEY_LENGTH ); else
162                 metadata.put( KEY_LENGTH, length );
163         }
164         
165         public void setLength(Range range) {
166                 if (range == null) {
167                         this._length = null;
168                         this._lengthIsForStr = null;
169                         metadata.remove( KEY_LENGTH );
170                 } else {
171                         this._length = range;
172                         this._lengthIsForStr = range.toString();
173                         metadata.put( KEY_LENGTH, _lengthIsForStr );                                    
174                 }
175         }
176
177         @SuppressWarnings("unchecked")
178         @Override
179         public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
180                 if (reference==null) return (T) this;
181                 throw new ReferenceException(reference.getClass()+" is not a subreference of StringType");      
182         }
183         
184         
185         
186 }