View Javadoc

1   /*
2    * $Id: OWLElementInfo.java,v 1.8 2005/05/31 00:20:51 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9    /***
10    * @author jlerner
11    * @author tself
12    */
13  
14  package com.bbn.swede.core;
15  
16  
17  /***
18   * Common implementation for OWL element info.  Element info objects are used
19   * to access data that is only available after opening the OWL element's
20   * underlying resources
21   */
22  public class OWLElementInfo
23  {
24     /***
25      * The children of the OWL element this info represents.
26      */
27     protected IOWLElement[] _aelemChildren;
28     private static IOWLElement[] _aelemEmptyChildren = new IOWLElement[] {
29     };
30  
31     /***
32      * The OWL element this info represents.
33      */
34     protected IOWLElement _elem;
35     /***
36      * Creates a new info object for an OWL element.
37      * @param elem The element.
38      */
39     public OWLElementInfo(IOWLElement elem)
40     {
41        _elem = elem;
42        _aelemChildren = _aelemEmptyChildren;
43     }
44  
45     /***
46      * Returns the OWL element associated with this info object.
47      * @return The OWL element this info object is associated with.
48      */
49     public IOWLElement getElement()
50     {
51        return _elem;
52     }
53  
54     /***
55      * Adds an element to the list of children.
56      * @param elemChild The new child element.
57      */
58     public void addChild(IOWLElement elemChild)
59     {
60        elemChild.setParent(_elem);
61        if (_aelemChildren == _aelemEmptyChildren)
62        {
63           _aelemChildren = new IOWLElement[] {elemChild};
64           return;
65        }
66        IOWLElement[] elems = new IOWLElement[_aelemChildren.length + 1];
67        System.arraycopy(_aelemChildren, 0, elems, 0, _aelemChildren.length);
68        elems[_aelemChildren.length] = elemChild;
69        _aelemChildren = elems;
70     }
71  
72     /***
73      * Checks whether another element is a child of this one.
74      * @param elemChild The element to check
75      * @return <code>true</code> if <code>elemChild</code> is a child of this
76      *         element, otherwise <code>false</code>.
77      */
78     public boolean includesChild(IOWLElement elemChild)
79     {
80        for (int i = 0; i < _aelemChildren.length; i++)
81        {
82           if (_aelemChildren[i].equals(elemChild))
83           {
84              return true;
85           }
86        }
87        return false;
88     }
89  
90     /***
91      * Removes a single child from the element's list of children.
92      * @param elemChild The child to remove.
93      */
94     public void removeChild(IOWLElement elemChild)
95     {
96        if (includesChild(elemChild))
97        {
98           IOWLElement[] elems = new IOWLElement[_aelemChildren.length - 1];
99           for (int i = 0; i < _aelemChildren.length; i++)
100          {
101             if (!_aelemChildren[i].equals(elemChild))
102             {
103                elems[i] = _aelemChildren[i];
104             }
105             else
106             {
107                System.arraycopy(
108                   _aelemChildren,
109                   i + 1,
110                   elems,
111                   i,
112                   _aelemChildren.length - i - 1);
113                _aelemChildren = elems;
114                return;
115             }
116          }
117          _aelemChildren = elems;
118          return;
119       }
120    }
121 
122    /***
123     * Returns the children of this OWL element.
124     * @return An array (possibly empty) of child elements.
125     */
126    public IOWLElement[] getChildren()
127    {
128       return _aelemChildren;
129    }
130 
131    /***
132     * Sets an array of OWL elements as this element's children.  Any existing
133     * children of this element are disassociated from it.
134     * @param elems The array of children to associate
135     */
136    public void setChildren(IOWLElement[] elems)
137    {
138       _aelemChildren = elems;
139    }
140 
141 }