ArduinoAttribute.java
/*******************************************************************************
* jArduino: Arduino C++ Code Generation From Java
* Copyright 2020 Tony Washer
*
* 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 net.sourceforge.jarduino.message;
import java.util.Objects;
import net.sourceforge.jarduino.ArduinoException;
/**
* Attribute.
*/
public class ArduinoAttribute {
/**
* Interface for object that has attributes.
*/
public interface ArduinoAttrObject {
/**
* Obtain value for attribute.
* @param pAttr the attribute
* @return the value
*/
Object getAttrValue(ArduinoAttribute pAttr);
}
/**
* The Relationship Marker.
*/
static final String REL_MARKER = "REL_";
/**
* The NODE2MSG Marker.
*/
static final String NODE2MSG_MARKER = ArduinoNode.MARKER + ArduinoMessage.MARKER + REL_MARKER;
/**
* The NODE2SIG Marker.
*/
static final String NODE2SIG_MARKER = ArduinoNode.MARKER + ArduinoSignal.MARKER + REL_MARKER;
/**
* The name of the attribute.
*/
private final String theName;
/**
* The class of the attribute.
*/
private final ArduinoAttrClass theClass;
/**
* The type of the attribute.
*/
private final ArduinoAttrType theType;
/**
* The constraints.
*/
private ArduinoAttrConstraints theConstraints;
/**
* The default value.
*/
private Object theDefault;
/**
* Constructor.
* @param pName the name
* @param pClass the class
* @param pType the type
*/
ArduinoAttribute(final String pName,
final ArduinoAttrClass pClass,
final ArduinoAttrType pType) {
theName = pName;
theClass = pClass;
theType = pType;
}
/**
* Obtain the name.
* @return the name
*/
public String getName() {
return theName;
}
/**
* Obtain the class.
* @return the class
*/
public ArduinoAttrClass getAttrClass() {
return theClass;
}
/**
* Obtain the type.
* @return the type
*/
public ArduinoAttrType getAttrType() {
return theType;
}
/**
* Obtain the constraints.
* @return the constraints
*/
public ArduinoAttrConstraints getConstraints() {
return theConstraints;
}
/**
* Set the constraints.
* @param pConstraints the constraints
*/
void setConstraints(final ArduinoAttrConstraints pConstraints) {
theConstraints = pConstraints;
}
/**
* Obtain the default value.
* @return the default
*/
public Object getDefault() {
return theDefault;
}
/**
* Set the default value.
* @param pDefault the default
*/
void setDefault(final Object pDefault) {
theDefault = pDefault;
}
@Override
public boolean equals(final Object pThat) {
/* Handle trivial cases */
if (pThat == this) {
return true;
} else if (!(pThat instanceof ArduinoAttribute)) {
return false;
}
/* Access correctly */
final ArduinoAttribute myThat = (ArduinoAttribute) pThat;
/* Check name and owner */
return theName.equals(myThat.getName());
}
@Override
public int hashCode() {
return Objects.hashCode(theName);
}
@Override
public String toString() {
String myName = theName + ":" + theClass + ":" + theType;
if (theDefault != null) {
myName += "=" + theDefault;
}
return myName;
}
/**
* AttributeClass.
*/
public enum ArduinoAttrClass {
/**
* System.
*/
SYSTEM,
/**
* Node.
*/
NODE,
/**
* Message.
*/
MESSAGE,
/**
* Signal.
*/
SIGNAL,
/**
* NodeToMessage.
*/
NODE2MSG,
/**
* NodeToSignal.
*/
NODE2SIGNAL;
/**
* Is this a relation attribute?
* @return true/false
*/
public boolean isRelation() {
switch (this) {
case NODE2SIGNAL:
case NODE2MSG:
return true;
default:
return false;
}
}
/**
* Parse attributeClass.
* @param pAttrClass the attributeClass representation
* @return the attrClass
* @throws ArduinoException on error
*/
static ArduinoAttrClass parseAttrClass(final String pAttrClass) throws ArduinoException {
switch (pAttrClass) {
case ArduinoNode.MARKER:
return NODE;
case ArduinoMessage.MARKER:
return MESSAGE;
case ArduinoSignal.MARKER:
return SIGNAL;
case NODE2MSG_MARKER:
return NODE2MSG;
case NODE2SIG_MARKER:
return NODE2SIGNAL;
default:
if (pAttrClass.length() == 0
|| pAttrClass.charAt(0) != ArduinoChar.QUOTE) {
throw new ArduinoException("Invalid Attribute class", pAttrClass);
}
return SYSTEM;
}
}
}
/**
* AttributeType.
*/
public enum ArduinoAttrType {
/**
* Integer.
*/
INT,
/**
* Float.
*/
FLOAT,
/**
* Hex.
*/
HEX,
/**
* String.
*/
STRING,
/**
* ENUM.
*/
ENUM;
/**
* Parse attrType.
* @param pAttrType the attrType representation
* @return the attrType
* @throws ArduinoException on error
*/
static ArduinoAttrType parseAttrType(final String pAttrType) throws ArduinoException {
/* Loop through the values */
for (ArduinoAttrType myType : values()) {
if (pAttrType.equals(myType.name())) {
return myType;
}
}
throw new ArduinoException("Invalid AttrType", pAttrType);
}
}
}