/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.juniform;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author acherkashin
*/
public final class JUniformMutableObject extends JUniformObject
{
// ---------------------------------------------------------------------- //
// PROTECTED
// ---------------------------------------------------------------------- //
protected Object _value;
protected List<Object> _array;
protected Map<Object,Object> _map;
@Override
protected List<Object> _getArray() {
return _array;
}
@Override
protected Map<Object,Object> _getMap() {
return _map;
}
// ---------------------------------------------------------------------- //
// STATIC
// ---------------------------------------------------------------------- //
public static JUniformMutableObject newMap() {
return new JUniformMutableObject(new HashMap<Object,Object>());
}
public static JUniformMutableObject newArray() {
return new JUniformMutableObject(new ArrayList<Object>());
}
// ---------------------------------------------------------------------- //
// PUBLIC
// ---------------------------------------------------------------------- //
public JUniformMutableObject() {
this(null);
}
public JUniformMutableObject(Object value) {
super(value);
this.setValue(value);
}
@Override
public Object getValue() {
return _value;
}
@SuppressWarnings("unchecked")
public void setValue(Object value) {
_value = value;
if (_value instanceof List) {
_array = (List<Object>)_value;
} else {
_array = null;
}
if (_value instanceof Map) {
_map = (Map<Object,Object>)_value;
} else {
_map = null;
}
}
public boolean setProperty(Object key, Object value) {
if (_map == null) {
return false;
}
_map.put(key, value);
return true;
}
public boolean removeProperty(Object key) {
if (_map == null) {
return false;
}
_map.remove(key);
return true;
}
public boolean addElement(Object value) {
if (_array == null) {
return false;
}
_array.add(value);
return true;
}
public boolean removeElement(Object value) {
if (_array == null) {
return false;
}
_array.remove(value);
return true;
}
public boolean removeElement(int index) {
if (_array == null) {
return false;
}
_array.remove(index);
return true;
}
public boolean clearProperties() {
if (_map == null) {
return false;
}
_map.clear();
return true;
}
public boolean clearElements() {
if (_array == null) {
return false;
}
_array.clear();
return true;
}
}