
com.almworks.jira.structure.api2g.attribute.AttributeSpec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of structure-api Show documentation
Show all versions of structure-api Show documentation
Public API for the Structure Plugin for JIRA
package com.almworks.jira.structure.api2g.attribute;
import com.almworks.jira.structure.api2g.Limits;
import com.almworks.jira.structure.api2g.attribute.loader.AttributeLoader;
import com.almworks.jira.structure.util.*;
import com.atlassian.annotations.PublicApi;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.Collections;
import java.util.Map;
/**
* AttributeSpec is a composite ID of an attribute that can be requested from {@link StructureAttributeService}.
* It contains an attribute ID, expected value format and, possibly, arbitrary parameters.
*
* @param type of the value expected for this attribute
*/
@PublicApi
@Immutable
public class AttributeSpec {
@NotNull
private final String myId;
@NotNull
private final ValueFormat myFormat;
@NotNull
private final Map myParams;
private transient int myHashCode;
private transient SpecParams myParamsObject;
public AttributeSpec(String id, ValueFormat format) {
this(id, format, null);
}
public AttributeSpec(@NotNull String id, @NotNull ValueFormat format, @Nullable Map params) {
this(id, format, params, false);
}
AttributeSpec(@NotNull String id, @NotNull ValueFormat format, @Nullable Map params, boolean reuseParams) {
if (StringUtils.isBlank(id)) {
throw new IllegalArgumentException("attribute id must not be empty");
}
if (id.length() > Limits.MAX_SPEC_ID_LENGTH) {
throw new IllegalArgumentException("attribute id must not be longer than " + Limits.MAX_SPEC_ID_LENGTH + " chars");
}
if (id.indexOf(':') >= 0) {
throw new IllegalArgumentException("attribute id must not contain colon");
}
if (format == null) {
throw new IllegalArgumentException("attribute format must not be null");
}
myId = id;
myFormat = format;
Map map = reuseParams ? params : JsonMapUtil.copyParameters(params, true, true, true);
// todo optimize - this should be done in copyParameters(), however, it is a more generic method, so it should have some abstraction
myParams = AttributeSpecNormalization.normalizeParams(map != null ? map : Collections.emptyMap());
}
@NotNull
public String getId() {
return myId;
}
@NotNull
public ValueFormat getFormat() {
return myFormat;
}
@NotNull
public Map getParamsMap() {
return myParams;
}
@NotNull
public SpecParams getParams() {
SpecParams paramsObject = myParamsObject;
if (paramsObject == null) {
myParamsObject = paramsObject = new SpecParams(myParams);
}
return paramsObject;
}
public boolean is(String id, ValueFormat> format) {
return myId.equals(id) && myFormat.equals(format);
}
public boolean is(ValueFormat> format) {
return myFormat.equals(format);
}
/**
* Returns an attribute spec with the same ID and parameters, but with the given ValueFormat.
*/
public AttributeSpec as(ValueFormat format) {
return is(format) ? format.cast(this) : new AttributeSpec(myId, format, myParams);
}
/**
* Returns new attribute spec with added parameter.
*/
public AttributeSpec withParam(String name, Object value) {
return AttributeSpecBuilder.create(this)
.params().set(name, value).done()
.build();
}
public AttributeLoader cast(AttributeLoader> loader) {
if (loader == null) return null;
if (!getFormat().getValueClass().isAssignableFrom(loader.getAttributeSpec().getFormat().getValueClass())) {
throw new ClassCastException("cannot cast " + loader + " to " + this);
}
//noinspection unchecked
return (AttributeLoader) loader;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AttributeSpec that = (AttributeSpec) o;
if (!myFormat.equals(that.myFormat)) return false;
if (!myId.equals(that.myId)) return false;
if (!myParams.equals(that.myParams)) return false;
return true;
}
public int hashCode() {
int result = myHashCode;
if (result == 0) {
result = myId.hashCode();
result = 31 * result + myFormat.hashCode();
result = 31 * result + (myParams.hashCode());
myHashCode = result;
}
return result;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(myId);
if (!myParams.isEmpty()) {
builder.append(':').append(StructureUtil.toJson(myParams));
}
builder.append(':').append(myFormat);
return builder.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy