use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.
the class EnumBeanTest method main.
public static void main(String[] args) {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
try {
EnumBean enumBean = (EnumBean) bxmlSerializer.readObject(EnumBeanTest.class, "enum_bean.bxml");
System.out.println("Bean read OK - " + enumBean);
} catch (IOException e) {
e.printStackTrace();
} catch (SerializationException e) {
e.printStackTrace();
}
EnumBean enumBean = new EnumBean();
BeanAdapter ba = new BeanAdapter(enumBean);
ba.put("orientationField", Orientation.HORIZONTAL);
dumpField(enumBean, ba);
ba.put("orientationField", "vertical");
dumpField(enumBean, ba);
ba.put("orientation", Orientation.HORIZONTAL);
dumpSetter(enumBean, ba);
ba.put("orientation", Orientation.VERTICAL);
dumpSetter(enumBean, ba);
ba.put("orientation", null);
dumpSetter(enumBean, ba);
// Force an error to check the IllegalArgumentException message
// ba.put("orientation", Vote.APPROVE);
}
use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.
the class JSON method get.
/**
* Returns the value at a given path.
*
* @param <T> The type of value to expect.
* @param root The root object.
* @param keys The path to the value as a sequence of keys.
* @return The value at the given path.
*/
@SuppressWarnings("unchecked")
public static <T> T get(Object root, Sequence<String> keys) {
Utils.checkNull(keys, "keys");
Object value = root;
for (int i = 0, n = keys.getLength(); i < n; i++) {
if (value == null) {
break;
}
String key = keys.get(i);
Map<String, T> adapter = (Map<String, T>) (value instanceof java.util.Map ? new MapAdapter<>((java.util.Map<String, T>) value) : (value instanceof Map ? ((Map<String, T>) value) : new BeanAdapter(value)));
if (adapter.containsKey(key)) {
value = adapter.get(key);
} else if (value instanceof Sequence<?>) {
Sequence<Object> sequence = (Sequence<Object>) value;
value = sequence.get(Integer.parseInt(key));
} else if (value instanceof Dictionary<?, ?>) {
Dictionary<String, Object> dictionary = (Dictionary<String, Object>) value;
value = dictionary.get(key);
} else {
throw new IllegalArgumentException("Property \"" + key + "\" not found.");
}
}
return (T) value;
}
use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.
the class CSVSerializer method readItem.
@SuppressWarnings("unchecked")
private Object readItem(Reader reader) throws IOException, SerializationException {
Object item = null;
if (c != -1) {
// Instantiate the item
Dictionary<String, Object> itemDictionary;
try {
if (itemType instanceof ParameterizedType) {
ParameterizedType parameterizedItemType = (ParameterizedType) itemType;
Class<?> rawItemType = (Class<?>) parameterizedItemType.getRawType();
item = rawItemType.newInstance();
} else {
Class<?> classItemType = (Class<?>) itemType;
item = classItemType.newInstance();
}
if (item instanceof Dictionary<?, ?>) {
itemDictionary = (Dictionary<String, Object>) item;
} else {
itemDictionary = new BeanAdapter(item);
}
} catch (IllegalAccessException exception) {
throw new SerializationException(exception);
} catch (InstantiationException exception) {
throw new SerializationException(exception);
}
// Add values to the item
for (int i = 0, n = keys.getLength(); i < n; i++) {
String key = keys.get(i);
String value = readValue(reader);
if (value == null) {
throw new SerializationException("Error reading value for " + key + " from input stream.");
}
if (c == '\r' || c == '\n') {
if (i < n - 1) {
throw new SerializationException("Line data is incomplete.");
}
// Move to next char; if LF, move again
c = reader.read();
if (c == '\n') {
c = reader.read();
}
}
itemDictionary.put(key, value);
}
// Notify the listeners
if (csvSerializerListeners != null) {
csvSerializerListeners.readItem(this, item);
}
}
return item;
}
use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.
the class CSVSerializer method writeObject.
/**
* Writes values to a comma-separated value stream.
*
* @param items A list containing the data to write to the CSV file. List
* items must be instances of <tt>Dictionary<String, Objecti></tt>. The dictionary
* values will be written out in the order specified by the key sequence.
* @param writer The writer to which data will be written.
* @throws IOException for any errors during writing.
* @throws IllegalArgumentException for {@code null} input arguments.
*/
@SuppressWarnings("unchecked")
public void writeObject(List<?> items, Writer writer) throws IOException {
Utils.checkNull(items, "items");
Utils.checkNull(writer, "writer");
if (writeKeys) {
// Write keys as first line
for (int i = 0, n = keys.getLength(); i < n; i++) {
String key = keys.get(i);
if (i > 0) {
writer.append(",");
}
writer.append(key);
}
}
for (Object item : items) {
Dictionary<String, Object> itemDictionary;
if (item instanceof Dictionary<?, ?>) {
itemDictionary = (Dictionary<String, Object>) item;
} else {
itemDictionary = new BeanAdapter(item);
}
for (int i = 0, n = keys.getLength(); i < n; i++) {
String key = keys.get(i);
if (i > 0) {
writer.append(",");
}
Object value = itemDictionary.get(key);
if (value != null) {
String string = value.toString();
if (string.indexOf(',') >= 0 || string.indexOf('"') >= 0 || string.indexOf('\r') >= 0 || string.indexOf('\n') >= 0) {
writer.append('"');
if (string.indexOf('"') == -1) {
writer.append(string);
} else {
writer.append(string.replace("\"", "\"\""));
}
writer.append('"');
} else {
writer.append(string);
}
}
}
writer.append("\r\n");
}
writer.flush();
}
use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.
the class TableViewRowComparator method compare.
/**
* Compares two rows in a table view. If the column values implement
* {@link Comparable}, the {@link Comparable#compareTo(Object)} method will
* be used to compare the values. Otherwise, the values will be compared as
* strings using {@link Object#toString()}. If either value is
* <tt>null</tt>, it will be considered as less than the other value. If
* both values are <tt>null</tt>, they will be considered equal.
*/
@Override
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
int result;
TableView.SortDictionary sort = tableView.getSort();
if (sort.getLength() > 0) {
Dictionary<String, ?> row1;
if (o1 instanceof Dictionary<?, ?>) {
row1 = (Dictionary<String, ?>) o1;
} else {
row1 = new BeanAdapter(o1);
}
Dictionary<String, ?> row2;
if (o2 instanceof Dictionary<?, ?>) {
row2 = (Dictionary<String, ?>) o2;
} else {
row2 = new BeanAdapter(o2);
}
result = 0;
int n = sort.getLength();
int i = 0;
while (i < n && result == 0) {
Dictionary.Pair<String, SortDirection> pair = sort.get(i);
String columnName = pair.key;
SortDirection sortDirection = sort.get(columnName);
Object value1 = row1.get(columnName);
Object value2 = row2.get(columnName);
if (value1 == null && value2 == null) {
result = 0;
} else if (value1 == null) {
result = -1;
} else if (value2 == null) {
result = 1;
} else {
if (value1 instanceof Comparable<?>) {
result = ((Comparable<Object>) value1).compareTo(value2);
} else {
String s1 = value1.toString();
String s2 = value2.toString();
result = s1.compareTo(s2);
}
}
result *= (sortDirection == SortDirection.ASCENDING ? 1 : -1);
i++;
}
} else {
result = 0;
}
return result;
}
Aggregations