org.obolibrary.oboformat.model.Frame Maven / Gradle / Ivy
package org.obolibrary.oboformat.model;
import java.util.*;
import javax.annotation.Nullable;
import org.obolibrary.obo2owl.OboInOwlCardinalityTools;
import org.obolibrary.oboformat.parser.OBOFormatConstants.OboFormatTag;
/** The Class Frame. */
public class Frame {
/** The Enum FrameType. */
public enum FrameType {
//@formatter:off
/** HEADER. */ HEADER,
/** TERM. */ TERM,
/** TYPEDEF. */ TYPEDEF,
/** INSTANCE. */ INSTANCE,
/** ANNOTATION. */ ANNOTATION
//@formatter:on
}
/** The clauses. */
protected Collection clauses;
/** The id. */
protected String id;
/** The type. */
protected FrameType type;
/** Instantiates a new frame. */
public Frame() {
init();
}
/**
* Instantiates a new frame.
*
* @param type
* the type
*/
public Frame(FrameType type) {
init();
this.type = type;
}
/** Init clauses. */
protected final void init() {
clauses = new ArrayList<>();
}
/**
* freezing a frame signals that a frame has become quiescent, and that data
* structures can be adjusted to increase performance or reduce memory
* consumption. If a frozen frame is subsequently modified it will be thawed
* as necessary.
*/
public void freeze() {
if (clauses.isEmpty()) {
clauses = Collections.emptyList();
return;
}
for (Clause clause : clauses) {
clause.freeze();
}
if (clauses.size() == 1) {
clauses = Collections.singletonList(clauses.iterator().next());
return;
}
if (clauses instanceof ArrayList>) {
ArrayList> arrayList = (ArrayList>) clauses;
arrayList.trimToSize();
}
}
/**
* @return the type
*/
public FrameType getType() {
return type;
}
/**
* @param type
* the new type
*/
public void setType(FrameType type) {
this.type = type;
}
/**
* @return the id
*/
@Nullable
public String getId() {
return id;
}
/**
* @param id
* the new id
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the clauses
*/
public Collection getClauses() {
return clauses;
}
/**
* @param tag
* the tag
* @return the clauses for tag
*/
public List getClauses(@Nullable String tag) {
List cls = new ArrayList<>();
if (tag == null) {
return cls;
}
for (Clause cl : clauses) {
if (tag.equals(cl.getTag())) {
cls.add(cl);
}
}
return cls;
}
/**
* @param tag
* the tag
* @return the clauses for tag
*/
public List getClauses(OboFormatTag tag) {
return getClauses(tag.getTag());
}
/**
* @param tag
* the tag
* @return null if no value set, otherwise first value
*/
@Nullable
public Clause getClause(@Nullable String tag) {
if (tag == null) {
return null;
}
for (Clause cl : clauses) {
if (tag.equals(cl.getTag())) {
return cl;
}
// TODO - throw exception if more than one clause of this type?
}
return null;
}
/**
* @param tag
* the tag
* @return the clause for tag
*/
@Nullable
public Clause getClause(OboFormatTag tag) {
return getClause(tag.getTag());
}
/**
* @param clauses
* the new clauses
*/
public void setClauses(Collection clauses) {
this.clauses = clauses;
}
/**
* @param cl
* the clause
*/
public void addClause(Clause cl) {
if (!(clauses instanceof ArrayList)) {
Collection tmp = new ArrayList<>(clauses.size() + 1);
tmp.addAll(clauses);
clauses = tmp;
}
clauses.add(cl);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Frame(");
sb.append(id);
sb.append(' ');
clauses.forEach(cl -> sb.append(cl).append(' '));
sb.append(')');
return sb.toString();
}
/**
* @param tag
* the tag
* @return the tag value for tag
*/
@Nullable
public Object getTagValue(String tag) {
Clause clause = getClause(tag);
if (clause == null) {
return null;
}
return clause.getValue();
}
/**
* @param tag
* the tag
* @return the tag value for tag
*/
@Nullable
public Object getTagValue(OboFormatTag tag) {
return getTagValue(tag.getTag());
}
/**
* @param
* the generic type
* @param tag
* the tag
* @param cls
* the cls
* @return the tag value for tag and class
*/
@Nullable
public T getTagValue(String tag, Class cls) {
Clause clause = getClause(tag);
if (clause == null) {
return null;
}
Object value = clause.getValue();
if (value.getClass().isAssignableFrom(cls)) {
return cls.cast(value);
}
return null;
}
/**
* @param
* the generic type
* @param tag
* the tag
* @param cls
* the cls
* @return the tag value for tag and class
*/
@Nullable
public T getTagValue(OboFormatTag tag, Class cls) {
return getTagValue(tag.getTag(), cls);
}
/**
* @param tag
* the tag
* @return the tag values for tag
*/
public Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy