Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Concept
- {
- private String id;
- protected Concept( String anId )
- {
- if ( anId == null )
- {
- throw new NullPointerException( "id must not be null" );
- }
- id = anId;
- }
- public String getId()
- {
- return id;
- }
- public void setId( final String id ) //changed
- {
- this.id = id;
- }
- public boolean equals( Object other )
- {
- return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept) other ).id );
- }
- public String toString()
- { return "Concept(" + id + ")";
- }
- }
- public class ConceptA extends Concept
- {
- private final Concept parent;
- public ConceptA( String anId, Concept aParent )
- {
- super( anId );
- parent = aParent;
- }
- public Concept getParent()
- {
- return parent;
- }
- public String toString()
- {
- return "ConceptA{" + getId() + ", parent=" + parent + '}';
- }
- }
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Iterator;
- public class ConceptB extends ConceptA
- {
- private final Set children;
- public ConceptB( final String anId, final Concept aParent )
- {
- super( anId, aParent );
- children = new HashSet();
- }
- public int getCount()
- {
- return children.size();
- }
- public void addChild( Concept aChild )
- {
- children.add( aChild );
- }
- public void removeChild( Concept aChild )
- {
- children.remove( aChild );
- }
- public Iterator getChildren()
- {
- return children.iterator();
- }
- public int getFamilySize()
- {
- int count = children.size();
- for ( Iterator iter = getChildren(); iter.hasNext(); )
- {
- count += ( (ConceptB) iter.next() ).getFamilySize();
- }
- return count;
- }
- public int getAncestorCount()
- {
- int count = 0;
- Concept ancestor = getParent();
- while ( ancestor != null )
- {
- count++;
- if ( ancestor instanceof ConceptA )
- {
- ancestor = ( (ConceptA) ancestor ).getParent();
- }
- else
- {
- ancestor = null;
- }
- }
- return count;
- }
- public String toString()
- {
- return "ConceptB{" + getId() + ", parent=" + getParent() + ", children=" + children.size() + "}";
- }
- }
- package com.result.exam.a;
- public class ConceptC extends ConceptA
- {
- private static int nextSerialNo = 0;
- public static int getNextSerialNo()
- {
- return nextSerialNo++;
- }
- private final int serialNo;
- public ConceptC( String anId )
- {
- super( anId, null );
- serialNo = getNextSerialNo();
- }
- public int getSerialNo()
- {
- return serialNo;
- }
- public String toString()
- {
- return "ConceptC(" + getId() + ", " + serialNo + ")";
- }
- }
- public boolean equals(Object other) {
- if(this == other) {
- return true;
- }
- if(!(other instanceof Concept)) {
- return false;
- }
- Concept otherConcept = (Concept) other;
- return id.equals(otherConcept.id);
- }
- public void addChild( Concept aChild )
- public Set getChildren()
- {
- return Collections.unmodifiableSet(children);
- }
- public int getFamilySize()
- {
- int count = 0;
- Queue<Concept> queue = new LinkedList<Concept>();
- queue.addAll(this.getChildren());
- while (!queue.empty())
- {
- Concept concept = queue.poll();
- // count the Concept we just pulled off the queue
- count++;
- if (concept instanceof ConceptB)
- {
- // we need his children too
- queue.addAll(((ConceptB) concept).getChildren());
- }
- }
- return count;
- }
- public int getFamilySize() {
- int count = children.size();
- for (Iterator iter = getChildren(); iter.hasNext(); ) {
- Concept child = (Concept) iter.next();
- if (child instanceof ConceptB){
- count += ((ConceptB) child).getFamilySize();
- }
- }
- return count;
- Concept parent = new ConceptC("parent c");
- Concept concept1 = new ConceptA("a", null);
- Concept concept2 = new ConceptA("a", parent);
- System.out.println(concept1.equals(concept2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement