/*
* Copyright (C) 2001, 2002 Robert MacGrogan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Archive: SourceJammer$
* $FileName: BranchNode.java$
* $FileID: 4600$
*
* Last change:
* $AuthorName: Rob MacGrogan$
* $Date: 4/23/03 5:16 PM$
* $Comment: Replaced GPL header with LGPL header.$
*/
package org.sourcejammer.project.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.sourcejammer.project.Node;
import org.sourcejammer.project.NodeDoesNotExistException;
import org.sourcejammer.project.NodeExistsException;
import org.sourcejammer.project.view.Branch;
import org.sourcejammer.util.ConfigurationException;
/**
* Title: $FileName: BranchNode.java$
* @version $VerNum: 7$
* @author $AuthorName: Rob MacGrogan$<br><br>
*
* $Description: $<br>
* $KeyWordsOff: $<br>
*/
public class BranchNode{
private String identifier = null;
private long fileID = -1;
private long initialVersionID = -1;
private ArrayList children = new ArrayList();
private BranchNode parent = null;
public BranchNode(){
}
public Branch getBranchView(){
Branch view = new Branch();
view.setFileID(fileID);
view.setIdentifier(identifier);
view.setInitialVersionID(initialVersionID);
ArrayList viewChildren = new ArrayList();
Iterator itr = children.iterator();
while (itr.hasNext()){
BranchNode child = (BranchNode)itr.next();
Branch viewChild = child.getBranchView();
viewChildren.add(viewChild);
}
view.setChildArrayList(viewChildren);
return view;
}
protected void applyIndex(BranchNode branch){
if (parent == null){
throw new ConfigurationException("Attempt to add child to a BranchNode that does not yet have parent set. BranchNode must have a parent before it can have children.");
}
parent.applyIndex(branch);
}
public void addBranchNode(BranchNode node, long key)
throws NodeExistsException{
checkKey(key);
if (! isIdentifierUnique(node.identifier)){
throw new NodeExistsException("There is already a branch of this file with the identifier " + node.identifier + ".");
}
node.setParent(this);
children.add(node);
applyIndex(node);
}
public void checkKey(long key){
if (parent == null){
throw new ConfigurationException("Attempt to add child to a BranchNode that does not yet have parent set. BranchNode must have a parent before it can have children.");
}
parent.checkKey(key);
}
public Iterator subBranches(){
return children.iterator();
}
protected void setParent(BranchNode node){
parent = node;
}
protected boolean isIdentifierUnique(String identifier){
if (parent == null){
throw new ConfigurationException("Attempt to add child to a BranchNode that does not yet have parent set. BranchNode must have a parent before it can have children.");
}
boolean unique = parent.isIdentifierUnique(identifier);
return unique;
}
public BranchNode getBranchNode(int index)
throws NodeDoesNotExistException{
BranchNode node = null;
try{
node = (BranchNode)children.get(index);
}
catch (IndexOutOfBoundsException ex){
throw new NodeDoesNotExistException("There is no BranchNode with index " + index + " in BranchNode " + identifier);
}
return node;
}
/**
* Returns the fileID.
* @return long
*/
public long getFileID() {
return fileID;
}
/**
* Returns the identifier.
* @return String
*/
public String getIdentifier() {
return identifier;
}
/**
* Returns the initialVersionID.
* @return long
*/
public long getInitialVersionID() {
return initialVersionID;
}
/**
* Sets the fileID.
* @param fileID The fileID to set
*/
public void setFileID(long fileID) {
this.fileID = fileID;
}
/**
* Sets the identifier.
* @param identifier The identifier to set
*/
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
/**
* Sets the initialVersionID.
* @param initialVersionID The initialVersionID to set
*/
public void setInitialVersionID(long initialVersionID) {
this.initialVersionID = initialVersionID;
}
}
|