-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add way to add or remove object dynamically at client side
- Loading branch information
1 parent
dd02d25
commit da2a39e
Showing
9 changed files
with
279 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mObjectTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Sierra Wireless - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.client.resource; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import org.eclipse.leshan.client.resource.listener.ObjectListener; | ||
import org.eclipse.leshan.client.resource.listener.ObjectsListener; | ||
|
||
public class LwM2mObjectTree { | ||
|
||
private ObjectListener dispatcher = new ObjectListenerDispatcher(); | ||
private CopyOnWriteArrayList<ObjectsListener> listeners = new CopyOnWriteArrayList<>(); | ||
private ConcurrentHashMap<Integer, LwM2mObjectEnabler> objectEnablers = new ConcurrentHashMap<>(); | ||
|
||
public LwM2mObjectTree(LwM2mObjectEnabler... enablers) { | ||
this(Arrays.asList(enablers)); | ||
} | ||
|
||
public LwM2mObjectTree(Collection<? extends LwM2mObjectEnabler> enablers) { | ||
for (LwM2mObjectEnabler enabler : enablers) { | ||
LwM2mObjectEnabler previousEnabler = objectEnablers.putIfAbsent(enabler.getId(), enabler); | ||
if (previousEnabler != null) { | ||
throw new IllegalArgumentException( | ||
String.format("Can not add 2 enablers for the same id %d", enabler.getId())); | ||
} | ||
} | ||
for (LwM2mObjectEnabler enabler : enablers) { | ||
enabler.setListener(dispatcher); | ||
} | ||
} | ||
|
||
public void addListener(ObjectsListener listener) { | ||
listeners.add(listener); | ||
} | ||
|
||
public void removedListener(ObjectsListener listener) { | ||
listeners.remove(listener); | ||
} | ||
|
||
public Map<Integer, LwM2mObjectEnabler> getObjectEnablers() { | ||
return Collections.unmodifiableMap(objectEnablers); | ||
} | ||
|
||
public LwM2mObjectEnabler getObjectEnabler(int id) { | ||
return objectEnablers.get(id); | ||
} | ||
|
||
public void addObjectEnabler(LwM2mObjectEnabler enabler) { | ||
LwM2mObjectEnabler previousEnabler = objectEnablers.putIfAbsent(enabler.getId(), enabler); | ||
enabler.setListener(dispatcher); | ||
if (previousEnabler != null) { | ||
throw new IllegalArgumentException( | ||
String.format("Can not add 2 enablers for the same id %d", enabler.getId())); | ||
} | ||
for (ObjectsListener listener : listeners) { | ||
listener.objectAdded(enabler); | ||
} | ||
} | ||
|
||
public void removeObjectEnabler(int objectId) { | ||
LwM2mObjectEnabler removedEnabler = objectEnablers.remove(objectId); | ||
if (removedEnabler != null) { | ||
removedEnabler.setListener(null); | ||
for (ObjectsListener listener : listeners) { | ||
listener.objectRemoved(removedEnabler); | ||
} | ||
} | ||
} | ||
|
||
private class ObjectListenerDispatcher implements ObjectListener { | ||
@Override | ||
public void objectInstancesAdded(LwM2mObjectEnabler object, int... instanceIds) { | ||
for (ObjectsListener listener : listeners) { | ||
listener.objectInstancesAdded(object, instanceIds); | ||
} | ||
} | ||
|
||
@Override | ||
public void objectInstancesRemoved(LwM2mObjectEnabler object, int... instanceIds) { | ||
for (ObjectsListener listener : listeners) { | ||
listener.objectInstancesRemoved(object, instanceIds); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.