use of org.apache.pivot.collections.Map in project pivot by apache.
the class BindTest method testBean.
/**
* Tests returning a Java bean value.
*
* @throws IOException
* @throws SerializationException
*/
@Test
public void testBean() throws IOException, SerializationException {
JSONSerializer mapSerializer = new JSONSerializer();
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) mapSerializer.readObject(getClass().getResourceAsStream("map.json"));
JSONSerializer beanSerializer = new JSONSerializer(SampleBean1.class);
SampleBean1 typedMap = (SampleBean1) beanSerializer.readObject(getClass().getResourceAsStream("map.json"));
assertEquals((Integer) typedMap.getA(), (Integer) JSON.get(map, "a"));
assertEquals(typedMap.getB(), JSON.get(map, "b"));
assertEquals(typedMap.getC(), JSON.get(map, "c"));
assertEquals(typedMap.getD(), JSON.get(map, "d"));
assertEquals(typedMap.getE(), JSON.get(map, "e"));
assertEquals((Integer) typedMap.getI().getA(), (Integer) JSON.get(map, "i.a"));
Object k0 = typedMap.getK().get(0);
assertTrue(k0 instanceof SampleBean2);
assertEquals((Integer) typedMap.getK().get(0).getA(), (Integer) JSON.get(map, "k[0].a"));
}
use of org.apache.pivot.collections.Map 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.collections.Map in project pivot by apache.
the class PropertiesSerializer method writeObject.
/**
* Writes data to a properties stream.
*
* @param object An instance of {@link Map} containing the data to be
* written to the properties file. Keys must be strings, and values will be
* converted to strings.
* @param outputStream The output stream to which data will be written.
*/
@SuppressWarnings("unchecked")
@Override
public void writeObject(Map<?, ?> object, OutputStream outputStream) throws IOException, SerializationException {
Utils.checkNull(object, "object");
Utils.checkNull(outputStream, "outputStream");
Map<Object, Object> map = (Map<Object, Object>) object;
Properties properties = new Properties();
for (Object key : map) {
Object value = map.get(key);
if (key != null && value != null) {
properties.put(key, value.toString());
}
}
properties.store(outputStream, null);
}
use of org.apache.pivot.collections.Map in project pivot by apache.
the class TerraTheme method setDefaultStyles.
/**
* Set appropriate default styles for the given skin object, specified by the
* current theme.
*
* @param <T> The skin class whose type we are dealing with.
* @param skin The skin object of that type whose styles are to be set.
*/
public <T extends ComponentSkin> void setDefaultStyles(T skin) {
String className = skin.getClass().getSimpleName();
@SuppressWarnings("unchecked") Map<String, Object> styleMap = (Map<String, Object>) themeDefaultStyles.get(className);
if (styleMap == null) {
throw new IllegalArgumentException("Cannot find default styles for class " + className);
}
Component component = skin.getComponent();
Component.StyleDictionary styles = component.getStyles();
styles.putAll(styleMap);
}
use of org.apache.pivot.collections.Map in project pivot by apache.
the class TerraTheme method load.
private void load(URL location) {
Utils.checkNull(location, "location");
try (InputStream inputStream = location.openStream()) {
JSONSerializer serializer = new JSONSerializer();
@SuppressWarnings("unchecked") Map<String, ?> properties = (Map<String, ?>) serializer.readObject(inputStream);
font = Font.decode((String) properties.get(FONT_PROPERTY));
String defaultStylesName = (String) properties.get(DEFAULT_STYLES_PROPERTY);
if (defaultStylesName != null && !defaultStylesName.isEmpty()) {
defaultStylesFile = defaultStylesName;
}
String namedStylesName = (String) properties.get(NAMED_STYLES_PROPERTY);
if (namedStylesName != null && !namedStylesName.isEmpty()) {
namedStylesFile = namedStylesName;
}
@SuppressWarnings("unchecked") List<String> colorCodes = (List<String>) properties.get(COLORS_PROPERTY);
numberOfPaletteColors = colorCodes.getLength();
int numberOfColors = numberOfPaletteColors * 3;
colors = new ArrayList<>(numberOfColors);
Double mult = (Double) properties.get(COLOR_MULTIPLIER_PROPERTY);
if (mult != null) {
colorMultiplier = mult.floatValue();
}
themeIsDark = properties.getBoolean(THEME_IS_DARK_PROPERTY, false);
themeIsFlat = properties.getBoolean(THEME_IS_FLAT_PROPERTY, false);
transitionEnabled = properties.getBoolean(TRANSITION_ENABLED_PROPERTY, true);
for (String colorCode : colorCodes) {
Color baseColor = GraphicsUtilities.decodeColor(colorCode, "baseColor");
colors.add(darken(baseColor));
colors.add(baseColor);
colors.add(brighten(baseColor));
}
@SuppressWarnings("unchecked") Map<String, String> messageIconNames = (Map<String, String>) properties.get(MESSAGE_ICONS_PROPERTY);
messageIcons = new HashMap<>();
loadMessageIcons(messageIconNames, messageIcons);
@SuppressWarnings("unchecked") Map<String, String> smallMessageIconNames = (Map<String, String>) properties.get(SMALL_MESSAGE_ICONS_PROPERTY);
smallMessageIcons = new HashMap<>();
loadMessageIcons(smallMessageIconNames, smallMessageIcons);
if ((defaultBackgroundColor = getColorProperty(properties, DEFAULT_BACKGROUND_COLOR_PROPERTY)) == null) {
defaultBackgroundColor = super.getDefaultBackgroundColor();
}
if ((defaultForegroundColor = getColorProperty(properties, DEFAULT_FOREGROUND_COLOR_PROPERTY)) == null) {
defaultForegroundColor = super.getDefaultForegroundColor();
}
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
}
Aggregations