All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.databene.model.data.FeatureDescriptor Maven / Gradle / Ivy

Go to download

benerator is a framework for creating realistic and valid high-volume test data, used for testing (unit/integration/load) and showcase setup. Metadata constraints are imported from systems and/or configuration files. Data can imported from and exported to files and systems, anonymized or generated from scratch. Domain packages provide reusable generators for creating domain-specific data as names and addresses internationalizable in language and region. It is strongly customizable with plugins and configuration options.

There is a newer version: 0.9.8
Show newest version
package org.databene.model.data;

import org.databene.commons.Named;
import org.databene.commons.NullSafeComparator;
import org.databene.commons.Operation;
import org.databene.commons.StringUtil;
import org.databene.commons.collection.OrderedNameMap;
import org.databene.commons.converter.AnyConverter;
import org.databene.commons.converter.ConverterManager;
import org.databene.commons.converter.String2ConverterConverter;
import org.databene.commons.converter.ToStringConverter;

import java.util.List;

/**
 * Common parent class of all descriptors.

* Created: 17.07.2006 21:30:45 * @since 0.1 * @author Volker Bergmann */ public class FeatureDescriptor implements Named { public static final String NAME = "name"; static { ConverterManager.getInstance().registerConverterClass(String2ConverterConverter.class); } protected OrderedNameMap> details; // constructor ----------------------------------------------------------------------------------------------------- public FeatureDescriptor(String name) { this.details = new OrderedNameMap>(); addConstraint(NAME, String.class, null); setName(name); } // typed interface ------------------------------------------------------------------------------------------------- /* public void setParent(FeatureDescriptor parent) { this.parent = parent; } */ public String getName() { return (String) getDetailValue(NAME); } public void setName(String name) { setDetailValue(NAME, name); } // generic detail access ------------------------------------------------------------------------------------------- public boolean supportsDetail(String name) { return details.containsKey(name); } public Object getDeclaredDetailValue(String name) { return getConfiguredDetail(name).getValue(); } public Object getDetailValue(String name) { FeatureDetail detail = getConfiguredDetail(name); return detail.getValue(); } public void setDetailValue(String detailName, Object detailValue) { FeatureDetail detail = getConfiguredDetail(detailName); detail.setValue(AnyConverter.convert(detailValue, detail.getType())); } public List> getDetails() { return details.values(); } // java.lang overrides --------------------------------------------------------------------------------------------- @Override public String toString() { String name = getName(); if (StringUtil.isEmpty(name)) name = "anonymous"; return renderDetails(new StringBuilder(name)).toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final FeatureDescriptor that = (FeatureDescriptor) o; for (FeatureDetail detail : details.values()) { String detailName = detail.getName(); if (!NullSafeComparator.equals(detail.getValue(), that.getDetailValue(detailName))) return false; } return true; } @Override public int hashCode() { return (getClass().hashCode() * 29 /*+ (parent != null ? parent.hashCode() : 0)*/) * 29 + details.hashCode(); } // helpers --------------------------------------------------------------------------------------------------------- protected String renderDetails() { return renderDetails(new StringBuilder()).toString(); } protected StringBuilder renderDetails(StringBuilder builder) { builder.append("["); boolean empty = true; for (FeatureDetail descriptor : details.values()) if (descriptor.getValue() != null && !NAME.equals(descriptor.getName())) { if (!empty) builder.append(", "); empty = false; builder.append(descriptor.getName()).append("="); builder.append(ToStringConverter.convert(descriptor.getValue(), "[null]")); } return builder.append("]"); } protected Class getDetailType(String detailName) { FeatureDetail detail = details.get(detailName); if (detail == null) throw new UnsupportedOperationException("Feature detail not supported: " + detailName); return detail.getType(); } protected void addConfig(String name, Class type) { addConfig(name, type, false); } protected void addConfig(String name, Class type, boolean deprecated) { addDetail(name, type, false, deprecated, null); } protected void addConstraint(String name, Class type, Operation combinator) { addDetail(name, type, true, false, combinator); } protected void addDetail(String detailName, Class detailType, boolean constraint, boolean deprecated, Operation combinator) { this.details.put(detailName, new FeatureDetail(detailName, detailType, constraint, combinator)); } // generic property access ----------------------------------------------------------------------------------------- @SuppressWarnings("unchecked") public FeatureDetail getConfiguredDetail(String name) { if (!supportsDetail(name)) throw new UnsupportedOperationException("Feature detail '" + name + "' not supported in feature type: " + getClass().getName()); return (FeatureDetail) details.get(name); } }