Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. public abstract class Concept
  2. {
  3. private String id;
  4.  
  5. protected Concept( String anId )
  6. {
  7. if ( anId == null )
  8. {
  9. throw new NullPointerException( "id must not be null" );
  10. }
  11.  
  12. id = anId;
  13. }
  14.  
  15. public String getId()
  16. {
  17. return id;
  18. }
  19.  
  20. public void setId( final String id ) //changed
  21. {
  22. this.id = id;
  23. }
  24.  
  25. public boolean equals( Object other )
  26. {
  27. return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept) other ).id );
  28. }
  29.  
  30. public String toString()
  31. { return "Concept(" + id + ")";
  32. }
  33. }
  34.  
  35. public class ConceptA extends Concept
  36. {
  37. private final Concept parent;
  38.  
  39. public ConceptA( String anId, Concept aParent )
  40. {
  41. super( anId );
  42.  
  43. parent = aParent;
  44. }
  45.  
  46. public Concept getParent()
  47. {
  48. return parent;
  49. }
  50.  
  51. public String toString()
  52. {
  53. return "ConceptA{" + getId() + ", parent=" + parent + '}';
  54. }
  55. }
  56.  
  57. import java.util.Set;
  58. import java.util.HashSet;
  59. import java.util.Iterator;
  60.  
  61. public class ConceptB extends ConceptA
  62. {
  63. private final Set children;
  64.  
  65. public ConceptB( final String anId, final Concept aParent )
  66. {
  67. super( anId, aParent );
  68.  
  69. children = new HashSet();
  70. }
  71.  
  72. public int getCount()
  73. {
  74. return children.size();
  75. }
  76.  
  77. public void addChild( Concept aChild )
  78. {
  79. children.add( aChild );
  80. }
  81.  
  82. public void removeChild( Concept aChild )
  83. {
  84. children.remove( aChild );
  85. }
  86.  
  87. public Iterator getChildren()
  88. {
  89. return children.iterator();
  90. }
  91.  
  92. public int getFamilySize()
  93. {
  94. int count = children.size();
  95.  
  96. for ( Iterator iter = getChildren(); iter.hasNext(); )
  97. {
  98. count += ( (ConceptB) iter.next() ).getFamilySize();
  99. }
  100.  
  101. return count;
  102. }
  103.  
  104. public int getAncestorCount()
  105. {
  106. int count = 0;
  107. Concept ancestor = getParent();
  108.  
  109. while ( ancestor != null )
  110. {
  111. count++;
  112. if ( ancestor instanceof ConceptA )
  113. {
  114. ancestor = ( (ConceptA) ancestor ).getParent();
  115. }
  116. else
  117. {
  118. ancestor = null;
  119. }
  120. }
  121.  
  122. return count;
  123. }
  124.  
  125. public String toString()
  126. {
  127. return "ConceptB{" + getId() + ", parent=" + getParent() + ", children=" + children.size() + "}";
  128. }
  129. }
  130.  
  131. package com.result.exam.a;
  132.  
  133. public class ConceptC extends ConceptA
  134. {
  135. private static int nextSerialNo = 0;
  136.  
  137. public static int getNextSerialNo()
  138. {
  139. return nextSerialNo++;
  140. }
  141.  
  142. private final int serialNo;
  143.  
  144. public ConceptC( String anId )
  145. {
  146. super( anId, null );
  147.  
  148. serialNo = getNextSerialNo();
  149. }
  150.  
  151. public int getSerialNo()
  152. {
  153. return serialNo;
  154. }
  155.  
  156. public String toString()
  157. {
  158. return "ConceptC(" + getId() + ", " + serialNo + ")";
  159. }
  160. }
  161.  
  162. public boolean equals(Object other) {
  163. if(this == other) {
  164. return true;
  165. }
  166.  
  167. if(!(other instanceof Concept)) {
  168. return false;
  169. }
  170.  
  171. Concept otherConcept = (Concept) other;
  172.  
  173. return id.equals(otherConcept.id);
  174. }
  175.  
  176. public void addChild( Concept aChild )
  177.  
  178. public Set getChildren()
  179. {
  180. return Collections.unmodifiableSet(children);
  181. }
  182.  
  183. public int getFamilySize()
  184. {
  185. int count = 0;
  186.  
  187. Queue<Concept> queue = new LinkedList<Concept>();
  188.  
  189. queue.addAll(this.getChildren());
  190.  
  191. while (!queue.empty())
  192. {
  193. Concept concept = queue.poll();
  194. // count the Concept we just pulled off the queue
  195. count++;
  196.  
  197. if (concept instanceof ConceptB)
  198. {
  199. // we need his children too
  200. queue.addAll(((ConceptB) concept).getChildren());
  201. }
  202. }
  203.  
  204. return count;
  205. }
  206.  
  207. public int getFamilySize() {
  208. int count = children.size();
  209.  
  210. for (Iterator iter = getChildren(); iter.hasNext(); ) {
  211. Concept child = (Concept) iter.next();
  212. if (child instanceof ConceptB){
  213. count += ((ConceptB) child).getFamilySize();
  214. }
  215. }
  216.  
  217. return count;
  218.  
  219. Concept parent = new ConceptC("parent c");
  220. Concept concept1 = new ConceptA("a", null);
  221. Concept concept2 = new ConceptA("a", parent);
  222.  
  223. System.out.println(concept1.equals(concept2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement