2121import java .util .LinkedList ;
2222import java .util .List ;
2323import java .util .Map ;
24+ import java .util .Map .Entry ;
2425
2526/**
2627 * AngularObjectRegistry keeps all the object that binded to Angular Display System.
3132 * - Global scope : Shared to all notebook that uses the same interpreter group
3233 */
3334public class AngularObjectRegistry {
34- Map <String , Map <String , AngularObject >> registry = new HashMap <>();
35- private final String GLOBAL_KEY = "_GLOBAL_" ;
35+ Map <String , Map <String , AngularObject < Object > >> registry = new HashMap <>();
36+ private static final String GLOBAL_KEY = "_GLOBAL_" ;
3637 private AngularObjectRegistryListener listener ;
3738 private String interpreterGroupId ;
38- private AngularObjectListener angularObjectListener ;
39+ private AngularObjectListener < Object > angularObjectListener ;
3940
4041 public AngularObjectRegistry (final String interpreterGroupId ,
4142 final AngularObjectRegistryListener listener ) {
4243 this .interpreterGroupId = interpreterGroupId ;
4344 this .listener = listener ;
44- angularObjectListener = new AngularObjectListener () {
45- @ Override
46- public void updated (AngularObject updatedObject ) {
47- if (listener != null ) {
48- listener .onUpdateAngularObject (interpreterGroupId , updatedObject );
49- }
45+ angularObjectListener = (AngularObject <Object > updatedObject ) -> {
46+ if (listener != null ) {
47+ listener .onUpdateAngularObject (interpreterGroupId , updatedObject );
5048 }
5149 };
50+
5251 }
5352
5453 public AngularObjectRegistryListener getListener () {
@@ -68,7 +67,7 @@ public AngularObjectRegistryListener getListener() {
6867 * @param paragraphId paragraphId belongs to. null for notebook scope
6968 * @return AngularObject that added
7069 */
71- public AngularObject add (String name , Object o , String noteId , String paragraphId ) {
70+ public AngularObject < Object > add (String name , Object o , String noteId , String paragraphId ) {
7271 return add (name , o , noteId , paragraphId , true );
7372 }
7473
@@ -83,15 +82,11 @@ private String getRegistryKey(String noteId, String paragraphId) {
8382 }
8483 }
8584 }
86-
87- private Map <String , AngularObject > getRegistryForKey (String noteId , String paragraphId ) {
85+
86+ private Map <String , AngularObject < Object > > getRegistryForKey (String noteId , String paragraphId ) {
8887 synchronized (registry ) {
8988 String key = getRegistryKey (noteId , paragraphId );
90- if (!registry .containsKey (key )) {
91- registry .put (key , new HashMap <String , AngularObject >());
92- }
93-
94- return registry .get (key );
89+ return registry .computeIfAbsent (key , k -> new HashMap <>());
9590 }
9691 }
9792
@@ -109,12 +104,12 @@ private Map<String, AngularObject> getRegistryForKey(String noteId, String parag
109104 * @param emit skip firing onAdd event on false
110105 * @return AngularObject that added
111106 */
112- public AngularObject add (String name , Object o , String noteId , String paragraphId ,
107+ public AngularObject < Object > add (String name , Object o , String noteId , String paragraphId ,
113108 boolean emit ) {
114- AngularObject ao = createNewAngularObject (name , o , noteId , paragraphId );
109+ AngularObject < Object > ao = createNewAngularObject (name , o , noteId , paragraphId );
115110
116111 synchronized (registry ) {
117- Map <String , AngularObject > noteLocalRegistry = getRegistryForKey (noteId , paragraphId );
112+ Map <String , AngularObject < Object > > noteLocalRegistry = getRegistryForKey (noteId , paragraphId );
118113 noteLocalRegistry .put (name , ao );
119114 if (listener != null && emit ) {
120115 listener .onAddAngularObject (interpreterGroupId , ao );
@@ -124,12 +119,12 @@ public AngularObject add(String name, Object o, String noteId, String paragraphI
124119 return ao ;
125120 }
126121
127- protected AngularObject createNewAngularObject (String name , Object o , String noteId ,
122+ protected AngularObject < Object > createNewAngularObject (String name , Object o , String noteId ,
128123 String paragraphId ) {
129- return new AngularObject (name , o , noteId , paragraphId , angularObjectListener );
124+ return new AngularObject <> (name , o , noteId , paragraphId , angularObjectListener );
130125 }
131126
132- protected AngularObjectListener getAngularObjectListener () {
127+ protected AngularObjectListener < Object > getAngularObjectListener () {
133128 return angularObjectListener ;
134129 }
135130
@@ -141,7 +136,7 @@ protected AngularObjectListener getAngularObjectListener() {
141136 * @param paragraphId paragraphId belongs to. null for notebook scope
142137 * @return removed object. null if object is not found in registry
143138 */
144- public AngularObject remove (String name , String noteId , String paragraphId ) {
139+ public AngularObject < Object > remove (String name , String noteId , String paragraphId ) {
145140 return remove (name , noteId , paragraphId , true );
146141 }
147142
@@ -154,10 +149,10 @@ public AngularObject remove(String name, String noteId, String paragraphId) {
154149 * @param emit skip fireing onRemove event on false
155150 * @return removed object. null if object is not found in registry
156151 */
157- public AngularObject remove (String name , String noteId , String paragraphId , boolean emit ) {
152+ public AngularObject < Object > remove (String name , String noteId , String paragraphId , boolean emit ) {
158153 synchronized (registry ) {
159- Map <String , AngularObject > r = getRegistryForKey (noteId , paragraphId );
160- AngularObject o = r .remove (name );
154+ Map <String , AngularObject < Object > > r = getRegistryForKey (noteId , paragraphId );
155+ AngularObject < Object > o = r .remove (name );
161156 if (listener != null && emit ) {
162157 listener .onRemoveAngularObject (interpreterGroupId , o );
163158 }
@@ -177,8 +172,8 @@ public AngularObject remove(String name, String noteId, String paragraphId, bool
177172 */
178173 public void removeAll (String noteId , String paragraphId ) {
179174 synchronized (registry ) {
180- List <AngularObject > all = getAll (noteId , paragraphId );
181- for (AngularObject ao : all ) {
175+ List <AngularObject < Object > > all = getAll (noteId , paragraphId );
176+ for (AngularObject < Object > ao : all ) {
182177 remove (ao .getName (), noteId , paragraphId );
183178 }
184179 }
@@ -191,9 +186,9 @@ public void removeAll(String noteId, String paragraphId) {
191186 * @param paragraphId paragraphId that belongs to
192187 * @return angularobject. null when not found
193188 */
194- public AngularObject get (String name , String noteId , String paragraphId ) {
189+ public AngularObject < Object > get (String name , String noteId , String paragraphId ) {
195190 synchronized (registry ) {
196- Map <String , AngularObject > r = getRegistryForKey (noteId , paragraphId );
191+ Map <String , AngularObject < Object > > r = getRegistryForKey (noteId , paragraphId );
197192 return r .get (name );
198193 }
199194 }
@@ -204,17 +199,17 @@ public AngularObject get(String name, String noteId, String paragraphId) {
204199 * @param paragraphId paragraphId that belongs to
205200 * @return all angularobject in the scope
206201 */
207- public List <AngularObject > getAll (String noteId , String paragraphId ) {
208- List <AngularObject > all = new LinkedList <>();
202+ public List <AngularObject < Object > > getAll (String noteId , String paragraphId ) {
203+ List <AngularObject < Object > > all = new LinkedList <>();
209204 synchronized (registry ) {
210- Map <String , AngularObject > r = getRegistryForKey (noteId , paragraphId );
205+ Map <String , AngularObject < Object > > r = getRegistryForKey (noteId , paragraphId );
211206 if (r != null ) {
212207 all .addAll (r .values ());
213208 }
214209 }
215210 return all ;
216211 }
217-
212+
218213 /**
219214 * Get all angular object related to specific note.
220215 * That includes all global scope objects, notebook scope objects and paragraph scope objects
@@ -223,16 +218,16 @@ public List<AngularObject> getAll(String noteId, String paragraphId) {
223218 * @param noteId
224219 * @return
225220 */
226- public List <AngularObject > getAllWithGlobal (String noteId ) {
227- List <AngularObject > all = new LinkedList <>();
221+ public List <AngularObject < Object > > getAllWithGlobal (String noteId ) {
222+ List <AngularObject < Object > > all = new LinkedList <>();
228223 synchronized (registry ) {
229- Map <String , AngularObject > global = getRegistryForKey (null , null );
224+ Map <String , AngularObject < Object > > global = getRegistryForKey (null , null );
230225 if (global != null ) {
231226 all .addAll (global .values ());
232227 }
233- for (String key : registry .keySet ()) {
234- if (key .startsWith (noteId )) {
235- all .addAll (registry . get ( key ).values ());
228+ for (Entry < String , Map < String , AngularObject < Object >>> registryEntry : registry .entrySet ()) {
229+ if (registryEntry . getKey () .startsWith (noteId )) {
230+ all .addAll (registryEntry . getValue ( ).values ());
236231 }
237232 }
238233 }
@@ -243,14 +238,14 @@ public String getInterpreterGroupId() {
243238 return interpreterGroupId ;
244239 }
245240
246- public Map <String , Map <String , AngularObject >> getRegistry () {
241+ public Map <String , Map <String , AngularObject < Object > >> getRegistry () {
247242 return registry ;
248243 }
249244
250- public void setRegistry (Map <String , Map <String , AngularObject >> registry ) {
245+ public void setRegistry (Map <String , Map <String , AngularObject < Object > >> registry ) {
251246 this .registry = registry ;
252- for (Map <String , AngularObject > map : registry .values ()) {
253- for (AngularObject ao : map .values ()) {
247+ for (Map <String , AngularObject < Object > > map : registry .values ()) {
248+ for (AngularObject < Object > ao : map .values ()) {
254249 ao .setListener (angularObjectListener );
255250 }
256251 }
0 commit comments