[New] Added new LongTimedState

This commit is contained in:
Robert von Burg 2019-04-04 22:51:48 +02:00
parent 0d3acc13ff
commit e51f5705ad
12 changed files with 292 additions and 4 deletions

View File

@ -62,6 +62,7 @@ public class StrolchModelConstants {
public static final String SUFFIX_REF = "-Ref";
public static final String BAG_RELATIONS = "relations";
public static final String BAG_PARAMETERS = "parameters";
public static final String TYPE_PARAMETERS = "Parameters";
/**
* ID of the admin role which has access to all resources

View File

@ -20,6 +20,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import li.strolch.model.activity.Activity;
import li.strolch.model.json.StrolchRootElementToJsonVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.visitor.StrolchElementVisitor;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import li.strolch.model.xml.StrolchElementToXmlStringVisitor;
@ -174,4 +175,56 @@ public interface StrolchRootElement extends StrolchElement, PolicyContainer, Par
default Activity asActivity() {
return (Activity) this;
}
/**
* Set or add a parameter to this element from a {@link JsonObject}
*
* @param jsonObject
* the object from which to get the value
* @param bagId
* the bag ID on which to set the value
* @param bagName
* the name of the bag, if the bag is to be created
* @param bagType
* the type of the bag, if the bag is to be created
* @param paramId
* the ID of the parameter on which to set the value, and also the Json reference ID
* @param paramName
* the name of the parameter, if the parameter is to be created
* @param type
* the type of Parameter to create
* @param ignoreOnEmpty
* if true, and the json object is missing the field, then the parameter is not changed, otherwise the parameter
* is cleared if the json field is missing or null
*/
default void setOrAddParamFromFlatJson(JsonObject jsonObject, String bagId, String bagName, String bagType,
String paramId, String paramName, StrolchValueType type, boolean ignoreOnEmpty) {
if (!jsonObject.has(paramId) && ignoreOnEmpty)
return;
ParameterBag bag = getParameterBag(bagId);
if (bag == null) {
bag = new ParameterBag(bagId, bagName, bagType);
addParameterBag(bag);
}
Parameter<?> param = bag.getParameter(paramId);
boolean valueNotSet = !jsonObject.has(paramId) || jsonObject.get(paramId).isJsonNull();
if (param == null && valueNotSet)
return;
if (param == null) {
param = type.parameterInstance();
param.setId(paramId);
param.setName(paramName);
bag.addParameter(param);
}
if (valueNotSet) {
param.clear();
} else {
param.setValueFromString(jsonObject.get(paramId).getAsString());
}
}
}

View File

@ -152,14 +152,12 @@ public enum StrolchValueType {
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(
MessageFormat.format("TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
return new LongTimedState();
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(
MessageFormat.format("Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
return new LongValue(valueAsString);
}
@Override

View File

@ -151,6 +151,35 @@ public class Tags {
public static final String AGENT_VERSION = "agentVersion";
public static final String COMPONENT_VERSIONS = "componentVersions";
public static final String ERRORS = "errors";
public static final String OPERATING_SYSTEM = "operatingSystem";
public static final String OS_NAME = "osName";
public static final String OS_ARCH = "osArch";
public static final String OS_VERSION = "osVersion";
public static final String JAVA_VENDOR = "javaVendor";
public static final String JAVA_VERSION = "javaVersion";
public static final String AVAILABLE_PROCESSORS = "availableProcessors";
public static final String SYSTEM_LOAD_AVERAGE = "systemLoadAverage";
public static final String START_TIME = "startTime";
public static final String UPTIME = "uptime";
public static final String MEMORY = "memory";
public static final String TOTAL_PHYSICAL_MEMORY_SIZE = "totalPhysicalMemorySize";
public static final String FREE_PHYSICAL_MEMORY_SIZE = "freePhysicalMemorySize";
public static final String FREE_SWAP_SPACE_SIZE = "freeSwapSpaceSize";
public static final String COMMITTED_VIRTUAL_MEMORY_SIZE = "committedVirtualMemorySize";
public static final String HEAP_MEMORY_USAGE_INIT = "heapMemoryUsageInit";
public static final String HEAP_MEMORY_USAGE_USED = "heapMemoryUsageUsed";
public static final String HEAP_MEMORY_USAGE_MAX = "heapMemoryUsageMax";
public static final String HEAP_MEMORY_USAGE_COMMITTED = "heapMemoryUsageCommitted";
public static final String ROOTS = "roots";
public static final String PATH = "path";
public static final String USABLE_SPACE = "usableSpace";
public static final String USED_SPACE = "usedSpace";
public static final String FREE_SPACE = "freeSpace";
public static final String TOTAL_SPACE = "totalSpace";
}
public static class Audit {

View File

@ -305,6 +305,13 @@ public class StrolchElementToJsonVisitor implements StrolchElementVisitor<JsonEl
return stateToJsonFull(state);
}
@Override
public JsonElement visitLongState(LongTimedState state) {
if (isFlat())
return stateToJsonFlat(state);
return stateToJsonFull(state);
}
@Override
public JsonElement visitStringState(StringSetTimedState state) {
if (isFlat())

View File

@ -0,0 +1,57 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.timedstate;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.impl.LongValue;
import li.strolch.model.visitor.StrolchElementVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class LongTimedState extends AbstractStrolchTimedState<LongValue> {
public LongTimedState() {
super();
}
public LongTimedState(String id, String name) {
super(id, name);
}
@Override
public void setStateFromStringAt(Long time, String value) {
assertNotReadonly();
getTimeEvolution().setValueAt(time, new LongValue(value));
}
@Override
public String getType() {
return StrolchValueType.LONG.getType();
}
@Override
public <U> U accept(StrolchElementVisitor<U> visitor) {
return visitor.visitLongState(this);
}
@Override
public LongTimedState getClone() {
LongTimedState clone = new LongTimedState();
fillClone(clone);
return clone;
}
}

View File

@ -0,0 +1,119 @@
/*
* Copyright 2013 Martin Smock <smock.martin@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model.timevalue.impl;
import java.io.Serializable;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
/**
* {@link IValue} implementation to work with Long valued {@link ITimeValue} objects
*
* @author Martin Smock <smock.martin@gmail.com>
*/
public class LongValue implements IValue<Long>, Serializable {
public static final LongValue NEUTRAL = new LongValue(0L);
private Long value;
public LongValue(Long value) {
this.value = value;
}
public LongValue(String valueAsString) throws NumberFormatException {
this.value = Long.parseLong(valueAsString);
}
@Override
public String getType() {
return StrolchValueType.LONG.getType();
}
@Override
public LongValue add(Long o) {
this.value += o;
return this;
}
@Override
public boolean matches(IValue<Long> other) {
return this.value.equals(other.getValue());
}
@Override
public Long getValue() {
return this.value;
}
@Override
public LongValue getInverse() {
return new LongValue(-getValue());
}
@Override
public String getValueAsString() {
return this.value.toString();
}
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LongValue [value=");
sb.append(this.value);
sb.append("]");
return sb.toString();
}
@Override
public LongValue getCopy() {
return new LongValue(this.value);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.value == null) ? 0 : this.value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
LongValue other = (LongValue) obj;
if (this.value == null) {
if (other.value != null) {
return false;
}
} else if (!this.value.equals(other.value)) {
return false;
}
return true;
}
}

View File

@ -93,6 +93,11 @@ public interface IActivityElementVisitor<U> extends StrolchElementVisitor<U> {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());
}
@Override
default U visitLongState(LongTimedState state) {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());
}
@Override
default U visitStringState(StringSetTimedState state) {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());

View File

@ -67,6 +67,11 @@ public interface ParameterVisitor<U> extends StrolchElementVisitor<U> {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());
}
@Override
default U visitLongState(LongTimedState state) {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());
}
@Override
default U visitStringState(StringSetTimedState state) {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());

View File

@ -475,6 +475,13 @@ public class StrolchElementDeepEqualsVisitor implements StrolchElementVisitor<Li
return getMismatchedLocators();
}
@Override
public List<Locator> visitLongState(LongTimedState state) {
DBC.PRE.assertEquals("Can't compare apples with pairs =)", this.srcElement.getClass(), state.getClass());
deepEquals((StrolchTimedState<?>) this.srcElement, state);
return getMismatchedLocators();
}
@Override
public List<Locator> visitStringState(StringSetTimedState state) {
DBC.PRE.assertEquals("Can't compare apples with pairs =)", this.srcElement.getClass(), state.getClass());

View File

@ -62,6 +62,8 @@ public interface StrolchElementVisitor<U> extends StrolchVisitor {
U visitFloatState(FloatTimedState state);
U visitLongState(LongTimedState state);
U visitFloatListState(FloatListTimedState state);
U visitIntegerState(IntegerTimedState state);

View File

@ -105,6 +105,11 @@ public interface StrolchRootElementVisitor<U> extends StrolchElementVisitor<U> {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());
}
@Override
default U visitLongState(LongTimedState state) {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());
}
@Override
default U visitStringState(StringSetTimedState state) {
throw new UnsupportedOperationException(getClass().getName() + " can not handle " + state.getClass());