001package de.deepamehta.core.model; 002 003import de.deepamehta.core.RelatedTopic; 004import de.deepamehta.core.Topic; 005import java.util.List; 006 007 008 009public class TopicReferenceModel extends RelatedTopicModel { 010 011 // ---------------------------------------------------------------------------------------------------- Constructors 012 013 public TopicReferenceModel(long topicId) { 014 super(topicId); 015 } 016 017 public TopicReferenceModel(long topicId, AssociationModel relatingAssoc) { 018 super(topicId, relatingAssoc); 019 } 020 021 public TopicReferenceModel(String topicUri) { 022 super(topicUri); 023 } 024 025 public TopicReferenceModel(String topicUri, AssociationModel relatingAssoc) { 026 super(topicUri, relatingAssoc); 027 } 028 029 public TopicReferenceModel(long topicId, ChildTopicsModel relatingAssocChildTopics) { 030 super(topicId, new AssociationModel(relatingAssocChildTopics)); 031 } 032 033 public TopicReferenceModel(String topicUri, ChildTopicsModel relatingAssocChildTopics) { 034 super(topicUri, new AssociationModel(relatingAssocChildTopics)); 035 } 036 037 // -------------------------------------------------------------------------------------------------- Public Methods 038 039 public boolean isReferenceById() { 040 return getId() != -1; 041 } 042 043 public boolean isReferenceByUri() { 044 return getUri() != null && !getUri().equals(""); 045 } 046 047 // --- 048 049 /** 050 * Checks weather this reference refers to the given topic. 051 */ 052 public boolean isReferingTo(Topic topic) { 053 if (isReferenceById()) { 054 return getId() == topic.getId(); 055 } else if (isReferenceByUri()) { 056 return getUri().equals(topic.getUri()); 057 } else { 058 throw new RuntimeException("Invalid topic reference (" + this + ")"); 059 } 060 } 061 062 /** 063 * From the given topics finds the one this reference refers to. 064 */ 065 public RelatedTopic findReferencedTopic(List<RelatedTopic> topics) { 066 for (RelatedTopic topic : topics) { 067 if (isReferingTo(topic)) { 068 return topic; 069 } 070 } 071 return null; 072 } 073 074 // --- 075 076 @Override 077 public String toString() { 078 return "reference " + super.toString(); 079 } 080}