package dongfang.mkt.configuration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public abstract class ConfigurationSet
{
public static final int NUMBER_OF_VARIABLES =
8;
protected int eepromVersion
;
protected List<ParameterEntry
> entries =
new ArrayList<ParameterEntry
>();
public ConfigurationSet
() {
super();
}
public List<ParameterEntry
> getEntries
() {
return entries
;
}
protected abstract int correctLengthToExcludeName
(int length
);
protected abstract void setName
(int[] data,
int offset
);
protected abstract void outputName
(StringBuilder sb
);
public void setData
(int[] data
) throws IOException {
int offset =
0;
int dataLength = correctLengthToExcludeName
(data.
length);
if (dataLength
!= getByteCount
()) {
throw new IOException("The received number of databytes (" + dataLength +
") was not equal to the length of the declared parameter set(" + getByteCount
()+
").");
}
for (ParameterEntry entry : entries
) {
offset += entry.
setValue(data, offset
);
}
setName
(data, offset
);
}
public int getByteCount
() {
int result =
0;
for (ParameterEntry e : entries
) {
result += e.
getByteCount();
}
return result
;
}
public String toStringWithValues
() {
StringBuilder result =
new StringBuilder();
outputName
(result
);
for (ParameterEntry entry : entries
) {
result.
append(entry.
toStringWithValues() +
"\n");
}
return result.
toString();
}
public String generateDynamicSubstitutionCode
() {
StringBuilder result =
new StringBuilder(
"const MMXLATION XLATIONS[] = {\n");
boolean hasEntries =
false;
for (ParameterEntry entry : entries
) {
if (entry
instanceof DynamicByteEntry
) {
if (hasEntries
)
result.
append(",\n");
else
hasEntries =
true;
DynamicByteEntry de =
(DynamicByteEntry
) entry
;
result.
append("{offsetof(ParamSet_t, ");
result.
append(de.
getStaticCodeName());
result.
append("), offsetof(DynamicParams_t, ");
result.
append(de.
getDynamicCodeName());
result.
append("),");
result.
append(Integer.
toString(de.
getMinValue()));
result.
append(",");
result.
append(Integer.
toString(de.
getMaxValue()));
result.
append("}");
}
}
result.
append("};");
return result.
toString();
}
}