-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1451: Support Negative value for GT and LT attributes
- Loading branch information
1 parent
16f69b1
commit 953687f
Showing
5 changed files
with
189 additions
and
14 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
81 changes: 81 additions & 0 deletions
81
...main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.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,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.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.link.lwm2m.attributes; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.leshan.core.parser.StringParser; | ||
|
||
/** | ||
* A Generic Attribute of type Double (for positive value only). | ||
*/ | ||
public class PositiveDoubleAttributeModel extends LwM2mAttributeModel<Double> { | ||
|
||
public PositiveDoubleAttributeModel(String coRELinkParam, Attachment attachment, | ||
Set<AssignationLevel> assignationLevels, AccessMode accessMode, AttributeClass attributeClass) { | ||
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); | ||
} | ||
|
||
@Override | ||
public String getInvalidValueCause(Double value) { | ||
if (value < 0) { | ||
return String.format("'%s' attribute value must not be negative", getName()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1*DIGIT ["." 1*DIGIT] | ||
* </pre> | ||
*/ | ||
@Override | ||
public <E extends Throwable> LwM2mAttribute<Double> consumeAttributeValue(StringParser<E> parser) throws E { | ||
// parse Value | ||
int start = parser.getPosition(); | ||
parser.consumeDIGIT(); | ||
while (parser.nextCharIsDIGIT()) { | ||
parser.consumeNextChar(); | ||
} | ||
if (parser.nextCharIs('.')) { | ||
parser.consumeNextChar(); | ||
parser.consumeDIGIT(); | ||
while (parser.nextCharIsDIGIT()) { | ||
parser.consumeNextChar(); | ||
} | ||
} | ||
int end = parser.getPosition(); | ||
|
||
// create attribute | ||
String strValue = parser.substring(start, end); | ||
try { | ||
return new LwM2mAttribute<Double>(this, Double.parseDouble(strValue)); | ||
} catch (NumberFormatException e) { | ||
parser.raiseException(e, "%s value '%s' is not a valid positive double in %s", getName(), strValue, | ||
parser.getStringToParse()); | ||
return null; | ||
} catch (IllegalArgumentException e) { | ||
parser.raiseException(e, "%s value '%s' is not a valid positive double in %s", getName(), strValue, | ||
parser.getStringToParse()); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public LwM2mAttribute<Double> createEmptyAttribute() { | ||
return new LwM2mAttribute<Double>(this); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...c/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.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,74 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.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.link.lwm2m.attributes; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.leshan.core.parser.StringParser; | ||
|
||
/** | ||
* A Generic Attribute of type Long (for positive value only). | ||
*/ | ||
public class PositiveLongAttributeModel extends LwM2mAttributeModel<Long> { | ||
|
||
public PositiveLongAttributeModel(String coRELinkParam, Attachment attachment, | ||
Set<AssignationLevel> assignationLevels, AccessMode accessMode, AttributeClass attributeClass) { | ||
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); | ||
} | ||
|
||
@Override | ||
public String getInvalidValueCause(Long value) { | ||
if (value < 0) { | ||
return String.format("'%s' attribute value must not be negative", getName()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1 * DIGIT | ||
* </pre> | ||
*/ | ||
@Override | ||
public <E extends Throwable> LwM2mAttribute<Long> consumeAttributeValue(StringParser<E> parser) throws E { | ||
// parse Value | ||
int start = parser.getPosition(); | ||
parser.consumeDIGIT(); | ||
while (parser.nextCharIsDIGIT()) { | ||
parser.consumeNextChar(); | ||
} | ||
int end = parser.getPosition(); | ||
|
||
// create attribute | ||
String strValue = parser.substring(start, end); | ||
try { | ||
return new LwM2mAttribute<Long>(this, Long.parseLong(strValue)); | ||
} catch (NumberFormatException e) { | ||
parser.raiseException(e, "%s value '%s' is not a valid positive long in %s", getName(), strValue, | ||
parser.getStringToParse()); | ||
return null; | ||
} catch (IllegalArgumentException e) { | ||
parser.raiseException(e, "%s value '%s' is not a value positive long in %s", getName(), strValue, | ||
parser.getStringToParse()); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public LwM2mAttribute<Long> createEmptyAttribute() { | ||
return new LwM2mAttribute<Long>(this); | ||
} | ||
} |