use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.
the class DefaultJavaFXListBinder method to.
@Override
public <T> Binding to(final ObservableList<T> dolphinList, final Function<? super T, ? extends S> converter) {
Assert.requireNonNull(dolphinList, "dolphinList");
Assert.requireNonNull(converter, "converter");
if (boundLists.containsKey(list)) {
throw new UnsupportedOperationException("A JavaFX list can only be bound to one Dolphin Platform list!");
}
boundLists.put(list, list);
final InternalListChangeListener<T> listChangeListener = new InternalListChangeListener<>(converter);
final Subscription subscription = dolphinList.onChanged(listChangeListener);
list.setAll(dolphinList.stream().map(converter).collect(Collectors.toList()));
ListChangeListener<S> readOnlyListener = c -> {
if (!listChangeListener.onChange) {
throw new UnsupportedOperationException("A JavaFX list that is bound to a dolphin list can only be modified by the binding!");
}
};
list.addListener(readOnlyListener);
return () -> {
subscription.unsubscribe();
list.removeListener(readOnlyListener);
boundLists.remove(list);
};
}
use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.
the class GarbageCollector method gc.
/**
* Calling this method triggers the garbage collection. For all dolphin beans (see {@link RemotingBean}) that
* are not referenced by a root bean (see {@link RemotingModel}) the defined {@link GarbageCollectionCallback} (see constructor)
* will be called.
*/
public synchronized void gc() {
if (!configuration.isUseGc()) {
LOG.trace("GC deactivated, no beans will be removed!");
return;
}
LOG.trace("Garbage collection started! GC will remove {} beans!", removeOnGC.size());
onRemoveCallback.onReject(removeOnGC.keySet());
for (Map.Entry<Instance, Object> entry : removeOnGC.entrySet()) {
Instance removedInstance = entry.getKey();
for (Property property : removedInstance.getProperties()) {
propertyToParent.remove(property);
}
for (ObservableList list : removedInstance.getLists()) {
listToParent.remove(list);
}
allInstances.remove(entry.getValue());
}
removedBeansCount = removedBeansCount + removeOnGC.size();
removeOnGC.clear();
gcCalls = gcCalls + 1;
LOG.trace("Garbage collection done! GC currently manages {} referenced beans!", allInstances.size());
}
use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.
the class GarbageCollector method getAllLists.
private IdentitySet<ObservableList> getAllLists(Object bean) {
IdentitySet<ObservableList> ret = new IdentitySet<>();
List<Field> fields = listFieldCache.get(bean.getClass());
if (fields == null) {
fields = new ArrayList<>();
for (Field field : ReflectionHelper.getInheritedDeclaredFields(bean.getClass())) {
if (ObservableList.class.isAssignableFrom(field.getType())) {
fields.add(field);
}
}
listFieldCache.put(bean.getClass(), fields);
}
for (Field field : fields) {
ret.add((ObservableList) ReflectionHelper.getPrivileged(field, bean));
}
return ret;
}
use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.
the class GarbageCollector method removeFromGC.
private void removeFromGC(Instance instance) {
LOG.trace("Bean of type {} removed from GC and will not be removed on next GC run", instance.getBean().getClass());
Object removed = removeOnGC.remove(instance);
LOG.trace("GC will remove {} beans at next GC run", removeOnGC.size());
if (removed != null) {
for (Property property : instance.getProperties()) {
Object value = property.get();
if (value != null && DolphinUtils.isDolphinBean(value.getClass())) {
Instance childInstance = getInstance(value);
removeFromGC(childInstance);
}
}
for (ObservableList list : instance.getLists()) {
for (Object value : list) {
if (value != null && DolphinUtils.isDolphinBean(value.getClass())) {
Instance childInstance = getInstance(value);
removeFromGC(childInstance);
}
}
}
}
}
use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.
the class ModelJsonSerializer method toJson.
public static JsonObject toJson(Object dolphinModel) {
if (dolphinModel == null) {
return null;
}
JsonObject jsonObject = new JsonObject();
for (Field field : ReflectionHelper.getInheritedDeclaredFields(dolphinModel.getClass())) {
if (DolphinUtils.isProperty(field.getType())) {
Property property = (Property) ReflectionHelper.getPrivileged(field, dolphinModel);
Object value = property.get();
if (value == null) {
jsonObject.add(field.getName(), null);
} else if (Number.class.isAssignableFrom(value.getClass()) || Double.TYPE.isAssignableFrom(value.getClass()) || Float.TYPE.isAssignableFrom(value.getClass()) || Long.TYPE.isAssignableFrom(value.getClass()) || Integer.TYPE.isAssignableFrom(value.getClass())) {
jsonObject.add(field.getName(), new JsonPrimitive((Number) value));
} else if (String.class.isAssignableFrom(value.getClass())) {
jsonObject.add(field.getName(), new JsonPrimitive((String) value));
} else if (Boolean.class.isAssignableFrom(value.getClass()) || Boolean.TYPE.isAssignableFrom(value.getClass())) {
jsonObject.add(field.getName(), new JsonPrimitive((Boolean) value));
} else {
jsonObject.add(field.getName(), toJson(value));
}
} else if (DolphinUtils.isObservableList(field.getType())) {
ObservableList list = (ObservableList) ReflectionHelper.getPrivileged(field, dolphinModel);
JsonArray jsonArray = new JsonArray();
for (Object value : list) {
if (value == null) {
// TODO
// jsonArray.add(null);
} else if (Number.class.isAssignableFrom(value.getClass()) || Double.TYPE.isAssignableFrom(value.getClass()) || Float.TYPE.isAssignableFrom(value.getClass()) || Long.TYPE.isAssignableFrom(value.getClass()) || Integer.TYPE.isAssignableFrom(value.getClass())) {
jsonArray.add(new JsonPrimitive((Number) value));
} else if (String.class.isAssignableFrom(value.getClass())) {
jsonArray.add(new JsonPrimitive((String) value));
} else if (Boolean.class.isAssignableFrom(value.getClass()) || Boolean.TYPE.isAssignableFrom(value.getClass())) {
jsonArray.add(new JsonPrimitive((Boolean) value));
} else {
jsonArray.add(toJson(value));
}
}
jsonObject.add(field.getName(), jsonArray);
}
}
return jsonObject;
}
Aggregations