/** * Sets the field value. * * @param field identifier of field * @param index value offset * @param attributes field properties * @param value field to update * @param force if <code>true</code> create the value */ private void setValue(int field, int index, int attributes, Object value, boolean force) { try { checkType(field, value); PIMField pimField = getField(field, false, true); if (pimField == null) { throw new IndexOutOfBoundsException("Empty field: " + field); } int currentValues = pimField.getValueCount(); if (index < 0 || index >= currentValues) { throw new IndexOutOfBoundsException( "0 <= index < " + currentValues + ", " + index + " not in range"); } if (!force) { checkReadOnlyFields(field); } if (value instanceof Integer) { checkIntValue(field, ((Integer) value).intValue()); } attributes = filterAttributes(field, attributes); pimField.setValue(attributes, value, index); modified = true; } catch (ClassCastException e) { throw new IllegalArgumentException("Wrong type for field"); } }
/** Sets default values for this item. */ protected void setDefaultValues() { int[] supportedFields = pimList.getSupportedFields(); for (int i = 0; i < supportedFields.length; i++) { int field = supportedFields[i]; PIMField pimField = getField(field, false, true); if ((pimField == null || pimField.getValueCount() == 0) && pimHandler.hasDefaultValue(pimListHandle, field)) { Object value = null; switch (pimList.getFieldDataType(field)) { case PIMItem.BOOLEAN: value = new Boolean(pimHandler.getDefaultBooleanValue(pimListHandle, field)); break; case PIMItem.BINARY: value = pimHandler.getDefaultBinaryValue(pimListHandle, field); break; case PIMItem.DATE: value = new Long(pimHandler.getDefaultDateValue(pimListHandle, field)); break; case PIMItem.INT: value = new Integer(pimHandler.getDefaultIntValue(pimListHandle, field)); break; case PIMItem.STRING: value = pimHandler.getDefaultStringValue(pimListHandle, field); break; case PIMItem.STRING_ARRAY: value = pimHandler.getDefaultStringArrayValue(pimListHandle, field); break; default: continue; } addValue(field, PIMItem.ATTR_NONE, value, false); } } }
/** * Gets current value. * * @param field identifier of field * @param index field identifier * @return requested field */ private Object getValue(int field, int index) { PIMField pimField = getField(field, false, true); if (pimField == null) { throw new IndexOutOfBoundsException("Empty field: " + field); } int currentValues = pimField.getValueCount(); if (index < 0 || index >= currentValues) { throw new IndexOutOfBoundsException( "0 <= index < " + currentValues + ", " + index + " not in range"); } return pimField.getValue(index); }
/** * Adds a value to a field. * * @param field identifier of field * @param attributes field properties * @param value field to update * @param force if <code>true</code> create the value * @throws FieldFullException if no more values can be added to the field */ private void addValue(int field, int attributes, Object value, boolean force) { checkType(field, value); PIMField pimField = getField(field, true, true); int maxValues = pimHandler.getMaximumValues(pimListHandle, field); int currentValues = pimField.getValueCount(); if (maxValues != -1 && currentValues >= maxValues) { throw new FieldFullException("Can only store " + maxValues + " in field", field); } if (!force) { checkReadOnlyFields(field); } if (value instanceof Integer) { checkIntValue(field, ((Integer) value).intValue()); } if (pimField.isScalar()) { // upgrade PIM field if (currentValues == 0) { pimField = new ScalarPIMField(); putField(field, pimField); } else { Object value0 = pimField.getValue(0); int attributes0 = pimField.getAttributes(0); pimField = new VectorPIMField(); pimField.addValue(attributes0, value0); putField(field, pimField); } } attributes = filterAttributes(field, attributes); pimField.addValue(attributes, value); modified = true; }
// JAVADOC COMMENT ELIDED public void removeValue(int field, int index) { PIMField pimField = getField(field, false, true); if (pimField == null) { throw new IndexOutOfBoundsException("Empty field: " + field); } int currentValues = pimField.getValueCount(); if (index < 0 || index >= currentValues) { throw new IndexOutOfBoundsException( "0 <= index < " + currentValues + ", " + index + " not in range"); } checkReadOnlyFields(field); pimField.removeValue(index); currentValues--; if (currentValues == 0) { removeField(field); } else if (currentValues == 1) { // downgrade field Object value = pimField.getValue(0); int attributes = pimField.getAttributes(0); pimField = new ScalarPIMField(); pimField.addValue(attributes, value); putField(field, pimField); } modified = true; }
// JAVADOC COMMENT ELIDED public int countValues(int field) { PIMField pimField = getField(field, false, true); return pimField == null ? 0 : pimField.getValueCount(); }
/** * Format the data for output. * * @return formatted data */ protected String formatData() { StringBuffer sb = new StringBuffer(); for (int i = 0; i < fieldValues.length; i++) { if (fieldValues[i].getValueCount() != 0) { PIMField pimField = fieldValues[i]; int field = fieldKeys[i]; int valueCount = pimField.getValueCount(); if (valueCount == 0) { continue; } if (i != 0) { sb.append(", "); } String label = pimHandler.getFieldLabel(pimListHandle, field); int dataType = pimHandler.getFieldDataType(pimListHandle, field); for (int j = 0; j < valueCount; j++) { sb.append(label); if (valueCount != 1) { sb.append("["); sb.append(j); sb.append("]"); } sb.append("="); Object value = pimField.getValue(j); if (value == null) { sb.append("null"); continue; } switch (dataType) { case STRING_ARRAY: { String[] aValue = (String[]) value; sb.append("["); for (int k = 0; k < aValue.length; k++) { if (k != 0) { sb.append(","); } sb.append(aValue[k]); } sb.append("]"); break; } case BINARY: { byte[] bValue = (byte[]) value; sb.append("<" + bValue.length + " bytes>"); break; } case DATE: { long dValue = ((Long) value).longValue(); sb.append(pimHandler.composeDateTime(dValue)); break; } default: sb.append(value); } } } } if (categories != null && categories.length != 0) { if (sb.length() > 0) { sb.append(", "); } sb.append("Categories=["); for (int i = 0; i < categories.length; i++) { if (i > 0) { sb.append(","); } sb.append(categories[i]); } sb.append("]"); } return sb.toString(); }