Search in sources :

Example 6 with BeanAdapter

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);
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) BeanAdapter(org.apache.pivot.beans.BeanAdapter) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 7 with BeanAdapter

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;
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Sequence(org.apache.pivot.collections.Sequence) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Map(org.apache.pivot.collections.Map)

Example 8 with BeanAdapter

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Dictionary(org.apache.pivot.collections.Dictionary) BeanAdapter(org.apache.pivot.beans.BeanAdapter)

Example 9 with BeanAdapter

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&lt;String, Objecti&gt;</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();
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) BeanAdapter(org.apache.pivot.beans.BeanAdapter)

Example 10 with BeanAdapter

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;
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) SortDirection(org.apache.pivot.wtk.SortDirection) BeanAdapter(org.apache.pivot.beans.BeanAdapter) TableView(org.apache.pivot.wtk.TableView)

Aggregations

BeanAdapter (org.apache.pivot.beans.BeanAdapter)14 Dictionary (org.apache.pivot.collections.Dictionary)6 Map (org.apache.pivot.collections.Map)4 Sequence (org.apache.pivot.collections.Sequence)4 ArrayList (org.apache.pivot.collections.ArrayList)3 List (org.apache.pivot.collections.List)3 SerializationException (org.apache.pivot.serialization.SerializationException)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 HashMap (org.apache.pivot.collections.HashMap)2 Component (org.apache.pivot.wtk.Component)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BeanMonitor (org.apache.pivot.beans.BeanMonitor)1 DefaultProperty (org.apache.pivot.beans.DefaultProperty)1 LinkedList (org.apache.pivot.collections.LinkedList)1 MapAdapter (org.apache.pivot.collections.adapter.MapAdapter)1