X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Frequest%2FAdapt.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Frequest%2FAdapt.java;h=3ca5a7362052b9a685d6968c93073c2af71ffe32;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java new file mode 100644 index 000000000..3ca5a7362 --- /dev/null +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java @@ -0,0 +1,78 @@ +package org.simantics.db.common.request; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.AdaptionException; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.exception.ValidationException; + +/** + * @author Tuukka Lehtonen + * + * @param the adaption target interface + */ +public class Adapt extends ResourceRead { + + final protected Class target; + final protected boolean allowNull; + final protected boolean uniqueResult; + + public Adapt(Resource resource, Class target) { + this(resource, target, false, false); + } + + public Adapt(Resource resource, Class target, boolean allowNull) { + this(resource, target, allowNull, false); + } + + public Adapt(Resource resource, Class target, boolean allowNull, boolean uniqueResult) { + super(resource); + assert target != null; + this.target = target; + this.allowNull = allowNull; + this.uniqueResult = uniqueResult; + } + + @Override + public T perform(ReadGraph graph) throws AdaptionException, ValidationException, ServiceException { + if (allowNull) { + if (uniqueResult) + return graph.getPossibleUniqueAdapter(resource, target); + return graph.getPossibleAdapter(resource, target); + } + if (uniqueResult) + return graph.adaptUnique(resource, target); + return graph.adapt(resource, target); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + (allowNull ? 1231 : 1237); + result = prime * result + (target.hashCode()); + result = prime * result + (uniqueResult ? 1303 : 1319); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + if (this == obj) + return true; + if (getClass() != obj.getClass()) + return false; + Adapt other = (Adapt) obj; + if (!resource.equals(other.resource)) + return false; + if (allowNull != other.allowNull) + return false; + if (!target.equals(other.target)) + return false; + if (uniqueResult != other.uniqueResult) + return false; + return true; + } + +} \ No newline at end of file