1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.runtime;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.GregorianCalendar;
24 import java.util.List;
25
26 import org.apache.chemistry.opencmis.client.api.Property;
27 import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
28 import org.apache.chemistry.opencmis.commons.enums.Cardinality;
29 import org.apache.chemistry.opencmis.commons.enums.PropertyType;
30 import org.apache.chemistry.opencmis.commons.impl.dataobjects.AbstractPropertyData;
31
32
33
34
35 public class PropertyImpl<T> extends AbstractPropertyData<T> implements Property<T>, Serializable {
36
37 private static final long serialVersionUID = 1L;
38 private final PropertyDefinition<T> propertyDefinition;
39
40
41
42
43 public PropertyImpl(PropertyDefinition<T> pd, List<T> values) {
44 if (pd == null) {
45 throw new IllegalArgumentException("Type must be set!");
46 }
47 if (values == null) {
48 throw new IllegalArgumentException("Value must be set!");
49 }
50 propertyDefinition = pd;
51 initialize(pd);
52 setValues(values);
53 }
54
55
56
57
58 public PropertyImpl(Property<T> property) {
59 if (property == null) {
60 throw new IllegalArgumentException("Source must be set!");
61 }
62
63 propertyDefinition = property.getDefinition();
64 initialize(property.getDefinition());
65 setValues(new ArrayList<T>(property.getValues()));
66 }
67
68 protected void initialize(PropertyDefinition<?> pd) {
69 setId(pd.getId());
70 setDisplayName(pd.getDisplayName());
71 setLocalName(pd.getLocalName());
72 setQueryName(pd.getQueryName());
73 }
74
75 @Override
76 public PropertyDefinition<T> getDefinition() {
77 return propertyDefinition;
78 }
79
80 @Override
81 public PropertyType getType() {
82 return propertyDefinition.getPropertyType();
83 }
84
85 @Override
86 @SuppressWarnings("unchecked")
87 public <U> U getValue() {
88 List<T> values = getValues();
89 if (propertyDefinition.getCardinality() == Cardinality.SINGLE) {
90 return values.isEmpty() ? null : (U) values.get(0);
91 } else {
92 return (U) values;
93 }
94 }
95
96 @Override
97 public String getValueAsString() {
98 List<T> values = getValues();
99 if (values.isEmpty()) {
100 return null;
101 }
102
103 return formatValue(values.get(0));
104 }
105
106 @Override
107 public String getValuesAsString() {
108 List<T> values = getValues();
109
110 StringBuilder result = new StringBuilder(128);
111 for (T value : values) {
112 if (result.length() > 0) {
113 result.append(", ");
114 }
115
116 result.append(formatValue(value));
117 }
118
119 return "[" + result.toString() + "]";
120 }
121
122 private String formatValue(T value) {
123 String result;
124
125 if (value == null) {
126 return null;
127 }
128
129 if (value instanceof GregorianCalendar) {
130 result = ((GregorianCalendar) value).getTime().toString();
131 } else {
132 result = value.toString();
133 }
134
135 return result;
136 }
137
138 @Override
139 public boolean isMultiValued() {
140 return propertyDefinition.getCardinality() == Cardinality.MULTI;
141 }
142 }