org.openmuc.jdlms.internal.DlmsEnumFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdlms Show documentation
Show all versions of jdlms Show documentation
jDLMS is a library implementing the DLMS/COSEM (IEC 62056) communication standard.
The newest version!
/*
* Copyright 2012-2023 Fraunhofer ISE
*
* This file is part of jDLMS.
* For more information visit http://www.openmuc.org
*
* jDLMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* jDLMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with jDLMS. If not, see .
*
*/
package org.openmuc.jdlms.internal;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import org.openmuc.jdlms.datatypes.DlmsEnumeration;
import org.openmuc.jdlms.internal.asn1.axdr.types.AxdrEnum;
import org.openmuc.jdlms.internal.asn1.axdr.types.AxdrInteger;
public class DlmsEnumFunctions {
public static & DlmsEnumeration> T enumValueFrom(long code, Class enumClass) {
return enumValueFrom(new AxdrInteger(code), enumClass);
}
public static AxdrEnum enumToAxdrEnum(T dlmsEnum) {
return new AxdrEnum(dlmsEnum.getCode());
}
public static & DlmsEnumeration, M extends AxdrInteger> T enumValueFrom(M code,
Class enumClass) {
T[] enumConstants = enumClass.getEnumConstants();
long maskedCode = code.getValue() & 0xFFFFFFFFFFFFFFFFL;
for (T constant : enumConstants) {
if (constant.getCode() == maskedCode) {
return constant;
}
}
// TODO
String message = MessageFormat.format("No constant with code {0} in {1}.", maskedCode,
enumClass.getSimpleName());
throw new IllegalArgumentException(message);
}
public static & DlmsEnumeration> Map generateEnumMap(Class enumClass) {
T[] enumConstants = enumClass.getEnumConstants();
HashMap map = new HashMap<>(enumConstants.length);
for (T constant : enumConstants) {
map.put(constant.getCode(), constant);
}
return map;
}
public static & DlmsEnumeration> T constantFor(Map mapping, Long code, T defaultValue) {
T constant = mapping.get(code);
if (constant != null) {
return constant;
}
else {
return defaultValue;
}
}
private DlmsEnumFunctions() {
}
}