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 + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement