Skip to content

Commit

Permalink
Remove raw types for AngularObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Reamer committed Sep 5, 2024
1 parent 13412ad commit c4c8918
Show file tree
Hide file tree
Showing 26 changed files with 200 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ protected InterpreterContext getIntpContext() {

@Override
public void onAddAngularObject(String interpreterGroupId,
AngularObject angularObject) {
AngularObject<Object> angularObject) {
onAdd.incrementAndGet();
}

@Override
public void onUpdateAngularObject(String interpreterGroupId,
AngularObject angularObject) {
AngularObject<Object> angularObject) {
onUpdate.incrementAndGet();
}

@Override
public void onRemoveAngularObject(String interpreterGroupId,
AngularObject angularObject) {
AngularObject<Object> angularObject) {
onRemove.incrementAndGet();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public void testAngularObjects() throws IOException, InterpreterNotFoundExceptio
note.run(p1.getId(), true);
assertEquals(Status.FINISHED, p1.getStatus());
// angular object is saved to InterpreterGroup's AngularObjectRegistry
List<AngularObject> angularObjects;
List<AngularObject<Object>> angularObjects;
try {
angularObjects = p1.getBindedInterpreter().getInterpreterGroup()
.getAngularObjectRegistry().getAll(note.getId(), null);
Expand Down Expand Up @@ -848,7 +848,7 @@ public void testAngularObjects() throws IOException, InterpreterNotFoundExceptio
p3.setText("%spark z.angularBindGlobal(\"name2\", \"world2\")");
note.run(p3.getId(), true);
assertEquals(Status.FINISHED, p3.getStatus());
List<AngularObject> globalAngularObjects;
List<AngularObject<Object>> globalAngularObjects;
try {
globalAngularObjects = p3.getBindedInterpreter().getInterpreterGroup()
.getAngularObjectRegistry().getAll(null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AngularObject<T> implements JsonSerializable {
private String name;
private T object;

private transient AngularObjectListener listener;
private transient AngularObjectListener<T> listener;
private transient List<AngularObjectWatcher> watchers = new LinkedList<>();

private String noteId; // noteId belonging to. null for global scope
Expand All @@ -67,7 +67,7 @@ public AngularObject() {
* @param listener event listener
*/
public AngularObject(String name, T o, String noteId, String paragraphId,
AngularObjectListener listener) {
AngularObjectListener<T> listener) {
this.name = name;
this.noteId = noteId;
this.paragraphId = paragraphId;
Expand Down Expand Up @@ -178,40 +178,37 @@ public void set(T o, boolean emit) {
emit();
}
LOGGER.debug("Update angular object: {} with value: {}", name, o);
final Logger LOGGER = LoggerFactory.getLogger(AngularObject.class);
List<AngularObjectWatcher> ws = new LinkedList<>();
synchronized (watchers) {
ws.addAll(watchers);
}

ExecutorService executor = ExecutorFactory.singleton().createOrGet("angularObjectWatcher", 50);
for (final AngularObjectWatcher w : ws) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
w.watch(before, after);
} catch (Exception e) {
LOGGER.error("Exception on watch", e);
}
Runnable task = () -> {
try {
w.watch(before, after);
} catch (Exception e) {
LOGGER.error("Exception on watch", e);
}
});
};
executor.submit(task);
}
}

/**
* Set event listener for this object
* @param listener
*/
public void setListener(AngularObjectListener listener) {
public void setListener(AngularObjectListener<T> listener) {
this.listener = listener;
}

/**
* Get event listener of this object
* @return event listener
*/
public AngularObjectListener getListener() {
public AngularObjectListener<T> getListener() {
return listener;
}

Expand Down Expand Up @@ -262,7 +259,8 @@ public String toJson() {
return GSON.toJson(this);
}

public static AngularObject<?> fromJson(String json) {
@SuppressWarnings("unchecked")
public static AngularObject<Object> fromJson(String json) {
return GSON.fromJson(json, AngularObject.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
/**
*
*/
public interface AngularObjectListener {
void updated(AngularObject updatedObject);
public interface AngularObjectListener<T> {
void updated(AngularObject<T> updatedObject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* AngularObjectRegistry keeps all the object that binded to Angular Display System.
Expand All @@ -31,24 +32,22 @@
* - Global scope : Shared to all notebook that uses the same interpreter group
*/
public class AngularObjectRegistry {
Map<String, Map<String, AngularObject>> registry = new HashMap<>();
private final String GLOBAL_KEY = "_GLOBAL_";
Map<String, Map<String, AngularObject<Object>>> registry = new HashMap<>();
private static final String GLOBAL_KEY = "_GLOBAL_";
private AngularObjectRegistryListener listener;
private String interpreterGroupId;
private AngularObjectListener angularObjectListener;
private AngularObjectListener<Object> angularObjectListener;

public AngularObjectRegistry(final String interpreterGroupId,
final AngularObjectRegistryListener listener) {
this.interpreterGroupId = interpreterGroupId;
this.listener = listener;
angularObjectListener = new AngularObjectListener() {
@Override
public void updated(AngularObject updatedObject) {
if (listener != null) {
listener.onUpdateAngularObject(interpreterGroupId, updatedObject);
}
angularObjectListener = (AngularObject<Object> updatedObject) -> {
if (listener != null) {
listener.onUpdateAngularObject(interpreterGroupId, updatedObject);
}
};

}

public AngularObjectRegistryListener getListener() {
Expand All @@ -68,7 +67,7 @@ public AngularObjectRegistryListener getListener() {
* @param paragraphId paragraphId belongs to. null for notebook scope
* @return AngularObject that added
*/
public AngularObject add(String name, Object o, String noteId, String paragraphId) {
public AngularObject<Object> add(String name, Object o, String noteId, String paragraphId) {
return add(name, o, noteId, paragraphId, true);
}

Expand All @@ -83,15 +82,11 @@ private String getRegistryKey(String noteId, String paragraphId) {
}
}
}
private Map<String, AngularObject> getRegistryForKey(String noteId, String paragraphId) {

private Map<String, AngularObject<Object>> getRegistryForKey(String noteId, String paragraphId) {
synchronized (registry) {
String key = getRegistryKey(noteId, paragraphId);
if (!registry.containsKey(key)) {
registry.put(key, new HashMap<String, AngularObject>());
}

return registry.get(key);
return registry.computeIfAbsent(key, k -> new HashMap<>());
}
}

Expand All @@ -109,12 +104,12 @@ private Map<String, AngularObject> getRegistryForKey(String noteId, String parag
* @param emit skip firing onAdd event on false
* @return AngularObject that added
*/
public AngularObject add(String name, Object o, String noteId, String paragraphId,
public AngularObject<Object> add(String name, Object o, String noteId, String paragraphId,
boolean emit) {
AngularObject ao = createNewAngularObject(name, o, noteId, paragraphId);
AngularObject<Object> ao = createNewAngularObject(name, o, noteId, paragraphId);

synchronized (registry) {
Map<String, AngularObject> noteLocalRegistry = getRegistryForKey(noteId, paragraphId);
Map<String, AngularObject<Object>> noteLocalRegistry = getRegistryForKey(noteId, paragraphId);
noteLocalRegistry.put(name, ao);
if (listener != null && emit) {
listener.onAddAngularObject(interpreterGroupId, ao);
Expand All @@ -124,12 +119,12 @@ public AngularObject add(String name, Object o, String noteId, String paragraphI
return ao;
}

protected AngularObject createNewAngularObject(String name, Object o, String noteId,
protected AngularObject<Object> createNewAngularObject(String name, Object o, String noteId,
String paragraphId) {
return new AngularObject(name, o, noteId, paragraphId, angularObjectListener);
return new AngularObject<>(name, o, noteId, paragraphId, angularObjectListener);
}

protected AngularObjectListener getAngularObjectListener() {
protected AngularObjectListener<Object> getAngularObjectListener() {
return angularObjectListener;
}

Expand All @@ -141,7 +136,7 @@ protected AngularObjectListener getAngularObjectListener() {
* @param paragraphId paragraphId belongs to. null for notebook scope
* @return removed object. null if object is not found in registry
*/
public AngularObject remove(String name, String noteId, String paragraphId) {
public AngularObject<Object> remove(String name, String noteId, String paragraphId) {
return remove(name, noteId, paragraphId, true);
}

Expand All @@ -154,10 +149,10 @@ public AngularObject remove(String name, String noteId, String paragraphId) {
* @param emit skip fireing onRemove event on false
* @return removed object. null if object is not found in registry
*/
public AngularObject remove(String name, String noteId, String paragraphId, boolean emit) {
public AngularObject<Object> remove(String name, String noteId, String paragraphId, boolean emit) {
synchronized (registry) {
Map<String, AngularObject> r = getRegistryForKey(noteId, paragraphId);
AngularObject o = r.remove(name);
Map<String, AngularObject<Object>> r = getRegistryForKey(noteId, paragraphId);
AngularObject<Object> o = r.remove(name);
if (listener != null && emit) {
listener.onRemoveAngularObject(interpreterGroupId, o);
}
Expand All @@ -177,8 +172,8 @@ public AngularObject remove(String name, String noteId, String paragraphId, bool
*/
public void removeAll(String noteId, String paragraphId) {
synchronized (registry) {
List<AngularObject> all = getAll(noteId, paragraphId);
for (AngularObject ao : all) {
List<AngularObject<Object>> all = getAll(noteId, paragraphId);
for (AngularObject<Object> ao : all) {
remove(ao.getName(), noteId, paragraphId);
}
}
Expand All @@ -191,9 +186,9 @@ public void removeAll(String noteId, String paragraphId) {
* @param paragraphId paragraphId that belongs to
* @return angularobject. null when not found
*/
public AngularObject get(String name, String noteId, String paragraphId) {
public AngularObject<Object> get(String name, String noteId, String paragraphId) {
synchronized (registry) {
Map<String, AngularObject> r = getRegistryForKey(noteId, paragraphId);
Map<String, AngularObject<Object>> r = getRegistryForKey(noteId, paragraphId);
return r.get(name);
}
}
Expand All @@ -204,17 +199,17 @@ public AngularObject get(String name, String noteId, String paragraphId) {
* @param paragraphId paragraphId that belongs to
* @return all angularobject in the scope
*/
public List<AngularObject> getAll(String noteId, String paragraphId) {
List<AngularObject> all = new LinkedList<>();
public List<AngularObject<Object>> getAll(String noteId, String paragraphId) {
List<AngularObject<Object>> all = new LinkedList<>();
synchronized (registry) {
Map<String, AngularObject> r = getRegistryForKey(noteId, paragraphId);
Map<String, AngularObject<Object>> r = getRegistryForKey(noteId, paragraphId);
if (r != null) {
all.addAll(r.values());
}
}
return all;
}

/**
* Get all angular object related to specific note.
* That includes all global scope objects, notebook scope objects and paragraph scope objects
Expand All @@ -223,16 +218,16 @@ public List<AngularObject> getAll(String noteId, String paragraphId) {
* @param noteId
* @return
*/
public List<AngularObject> getAllWithGlobal(String noteId) {
List<AngularObject> all = new LinkedList<>();
public List<AngularObject<Object>> getAllWithGlobal(String noteId) {
List<AngularObject<Object>> all = new LinkedList<>();
synchronized (registry) {
Map<String, AngularObject> global = getRegistryForKey(null, null);
Map<String, AngularObject<Object>> global = getRegistryForKey(null, null);
if (global != null) {
all.addAll(global.values());
}
for (String key : registry.keySet()) {
if (key.startsWith(noteId)) {
all.addAll(registry.get(key).values());
for (Entry<String, Map<String, AngularObject<Object>>> registryEntry : registry.entrySet()) {
if (registryEntry.getKey().startsWith(noteId)) {
all.addAll(registryEntry.getValue().values());
}
}
}
Expand All @@ -243,14 +238,14 @@ public String getInterpreterGroupId() {
return interpreterGroupId;
}

public Map<String, Map<String, AngularObject>> getRegistry() {
public Map<String, Map<String, AngularObject<Object>>> getRegistry() {
return registry;
}

public void setRegistry(Map<String, Map<String, AngularObject>> registry) {
public void setRegistry(Map<String, Map<String, AngularObject<Object>>> registry) {
this.registry = registry;
for (Map<String, AngularObject> map : registry.values()) {
for (AngularObject ao : map.values()) {
for (Map<String, AngularObject<Object>> map : registry.values()) {
for (AngularObject<Object> ao : map.values()) {
ao.setListener(angularObjectListener);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* Listener class for angular object operations, such as add, update, remove.
*/
public interface AngularObjectRegistryListener {
void onAddAngularObject(String interpreterGroupId, AngularObject angularObject);
void onUpdateAngularObject(String interpreterGroupId, AngularObject angularObject);
void onRemoveAngularObject(String interpreterGroupId, AngularObject angularObject);
void onAddAngularObject(String interpreterGroupId, AngularObject<Object> angularObject);

void onUpdateAngularObject(String interpreterGroupId, AngularObject<Object> angularObject);

void onRemoveAngularObject(String interpreterGroupId, AngularObject<Object> angularObject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.apache.zeppelin.resource.Resource;
import org.apache.zeppelin.resource.ResourcePool;
import org.apache.zeppelin.resource.ResourceSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Constructor;
Expand All @@ -38,7 +36,6 @@
* Load application
*/
public class ApplicationLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationLoader.class);

private final DependencyResolver depResolver;
private final ResourcePool resourcePool;
Expand Down Expand Up @@ -129,8 +126,7 @@ public Application load(HeliumPackage packageInfo, ApplicationContext context)
try {
Constructor<Application> constructor = appClass.getConstructor(ApplicationContext.class);

Application app = new ClassLoaderApplication(constructor.newInstance(context), cl);
return app;
return new ClassLoaderApplication(constructor.newInstance(context), cl);
} catch (Exception e) {
throw new ApplicationException(e);
} finally {
Expand Down
Loading

0 comments on commit c4c8918

Please sign in to comment.