]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/StringBinding.java
Use type reflection tools from databoard in objmap2.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / StringBinding.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.binding;
13
14 import java.util.IdentityHashMap;
15 import java.util.Set;
16 import java.util.regex.Pattern;
17
18 import org.simantics.databoard.accessor.reference.ChildReference;
19 import org.simantics.databoard.binding.error.BindingException;
20 import org.simantics.databoard.binding.error.RuntimeBindingException;
21 import org.simantics.databoard.binding.impl.BindingPrintContext;
22 import org.simantics.databoard.type.StringType;
23 import org.simantics.databoard.util.IdentityPair;
24 import org.simantics.databoard.util.Range;
25
26 /**
27  * This is a binding of String Type and a Java Object.
28  *
29  * @see StringType
30  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
31  */
32 public abstract class StringBinding extends Binding {
33
34         public StringBinding(StringType type) {
35                 this.type = type;
36         }
37         
38         public StringType type() {
39                 return (StringType) type;
40         }
41         
42         public abstract Object create(String value) throws BindingException;
43         
44         public Object createUnchecked(String value) throws RuntimeBindingException {
45                 try {
46                         return create(value);
47                 } catch (BindingException e) {
48                         throw new RuntimeBindingException(e);
49                 }
50         }
51         
52         @Override
53         public void readFrom(Binding srcBinding, Object src, Object dst)
54                         throws BindingException {
55                 String v = ((StringBinding)srcBinding).getValue(src);
56                 setValue(dst, v);
57         }
58         
59         public abstract String getValue(Object o) throws BindingException; 
60         public abstract void setValue(Object o, String newValue) throws BindingException; 
61         public abstract boolean isInstance(Object obj);
62         
63     @Override
64     public void accept(Visitor1 v, Object obj) {
65         v.visit(this, obj);        
66     }
67     
68     @Override
69     public <T> T accept(Visitor<T> v) {
70         return v.visit(this);
71     }
72
73     /**
74      * Assert obj is a valid StringType
75      * 
76      * This asserts that
77      *  1. pattern is valid
78      *  2. mime type is valid
79      *  3. length is valid
80      * 
81      * @throws BindingException
82      */
83     @Override
84     public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
85                 if (!isInstance(obj)) throw new BindingException("Not a correct instance");                             
86                 
87         StringType type = type();
88         Pattern p = type.getCompiledPattern();
89         String mimeType = type.getMimeType();
90         Range length = type.getLength();
91         if (p==null && length==null && mimeType==null) return;          
92         String str = getValue(obj);     
93         
94         // 1. Assert the pattern is valid
95         if (p!=null && !p.matcher(str).matches()) {
96                 throw new BindingException("The value ("+str+") doesn't match the pattern ("+p.toString()+").");
97         }
98         
99         // 2. Assert the mime type is valid
100         if (mimeType!=null) {                   
101 //              try {
102 //                      MimeType mt = new MimeType(mimeType);
103 //                      if (!mt.match(str)) 
104 //                              throw new BindingException("The object is not "+mimeType);
105 //              } catch (MimeTypeParseException mtpe) {
106 //                      throw new BindingException(mtpe);
107 //              }               
108         }
109         
110         // 3. Assert the length is valid
111         if (length!=null && !length.contains(str.length())) {
112                 throw new BindingException("Length ("+str.length()+") doesn't fit into the range "+length);
113         }
114     }
115
116     @Override
117     public int deepCompare(Object o1, Object o2,
118                 Set<IdentityPair<Object, Object>> compareHistory)
119                 throws BindingException {
120         String v1 = getValue(o1);
121         String v2 = getValue(o2);
122         return v1.compareTo(v2);        
123     }
124     
125     @Override
126     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
127         return getValue(value).hashCode();
128     }
129     
130     @Override
131         protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
132         ctx.b.append('"');
133         ctx.b.append(getValue(value));
134         ctx.b.append('"');
135     }
136     
137         @Override
138         public Binding getComponentBinding(ChildReference path) {
139                 if (path==null) return this;            
140                 throw new IllegalArgumentException();
141         }           
142         
143         @Override
144         public int getComponentCount() {
145                 return 0;
146         }
147         
148         @Override
149         public Binding getComponentBinding(int index) {
150                 throw new IllegalArgumentException();
151         }
152     
153 }