Skip to content

Commit

Permalink
Add LwM2mModelRepository, it can contain several version of same object
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 12, 2019
1 parent a8580a2 commit fe8512e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* 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.core.model;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.eclipse.leshan.util.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A collection of LWM2M object definitions which could contained several definitions of the same object in different
* version.
*/
public class LwM2mModelRepository {
private static final Logger LOG = LoggerFactory.getLogger(LwM2mModelRepository.class);

// This map contains all the object models available. Different version could be used.
// This map is indexed by a string composed of objectid and version (objectid##version)
private final Map<String, ObjectModel> objects;

public LwM2mModelRepository(ObjectModel... objectModels) {
this(Arrays.asList(objectModels));
}

public LwM2mModelRepository(Collection<ObjectModel> objectModels) {
if (objectModels == null) {
objects = new TreeMap<>();
} else {
Map<String, ObjectModel> map = new HashMap<>();
for (ObjectModel model : objectModels) {
String key = getKey(model);
if (key == null) {
new IllegalArgumentException(String.format("Model %s is invalid : object id is missing.", model));
}
ObjectModel old = map.put(key, model);
if (old != null) {
LOG.debug("Model already exists for object {} in version {}. Overriding it.", model.id,
model.getVersion());
}
}
objects = Collections.unmodifiableMap(map);
}
}

public ObjectModel getObjectModel(Integer objectId, String version) {
Validate.notNull(objectId, "objectid must not be null");
Validate.notNull(version, "version must not be null");

return objects.get(getKey(objectId, version));
}

private String getKey(ObjectModel objectModel) {
return getKey(objectModel.id, objectModel.getVersion());
}

private String getKey(Integer objectId, String version) {
if (objectId == null) {
return null;
}
return objectId + "##" + version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public boolean isOmaObject() {
return id >= OMA_OBJECT_MIN_ID && id <= OMA_OBJECT_MAX_ID;
}

/**
* @return the version and if the version is null or empty return the default value 1.0
* @see ObjectModel#DEFAULT_VERSION
*/
public String getVersion() {
if (version == null || version.isEmpty()) {
return ObjectModel.DEFAULT_VERSION;
}
return version;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand Down

0 comments on commit fe8512e

Please sign in to comment.