Save This Page
Home » jdo2-model-2.3-ea-src » org.apache.jdo.impl.model.java » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software 
   12    * distributed under the License is distributed on an "AS IS" BASIS, 
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   14    * See the License for the specific language governing permissions and 
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.jdo.impl.model.java;
   19   
   20   import java.util.Map;
   21   import java.util.Set;
   22   import java.util.HashMap;
   23   import java.util.HashSet;
   24   import java.io.InputStream;
   25   
   26   import org.apache.jdo.model.ModelException;
   27   import org.apache.jdo.model.java.JavaModel;
   28   import org.apache.jdo.model.java.JavaType;
   29   import org.apache.jdo.model.jdo.JDOModel;
   30   
   31   
   32   /**
   33    * Abstract super class for JavaModel implementations. 
   34    * It implements the jdoModel property and the parent/child relationship
   35    * between javaModels. It also provides a map of types managed by this
   36    * JavaModel (see {@link #types}). The AbstractJavaModel constructor
   37    * automatically adds all the predefined types to this map.
   38    * <p>
   39    * A non-abstract subclass must implement methods
   40    * {@link #getJavaType(String name)} and 
   41    * {@link #getInputStreamForResource(String resourceName)}.
   42    *
   43    * @author Michael Bouschen
   44    * @since JDO 1.0.1
   45    */
   46   abstract public class AbstractJavaModel
   47       implements JavaModel
   48   {
   49       /** Map of known JavaTypes. Key is the type name as a string. */
   50       protected Map types;
   51   
   52       /** The parent JavaModel. */
   53       protected JavaModel parent;
   54   
   55       /** The child JavaModels. */
   56       protected Set children = new HashSet();
   57   
   58       /** The corresponding JDOModel instance. */
   59       protected JDOModel jdoModel;
   60       
   61       /**
   62        * Constructor. It adds all predefined types to the cache of types
   63        * known by this model instance.
   64        * @see PredefinedType
   65        */
   66       protected AbstractJavaModel()
   67       {
   68           types = new HashMap(PredefinedType.getPredefinedTypes());
   69       }
   70   
   71       /** 
   72        * The method returns the JavaType instance for the specified type
   73        * name. A type name is unique within one JavaModel instance. The
   74        * method returns <code>null</code> if this model instance does not
   75        * know a type with the specified name.
   76        * @return a JavaType instance for the specified name or
   77        * <code>null</code> if not present in this model instance.
   78        */
   79       abstract public JavaType getJavaType(String name);
   80   
   81       /** 
   82        * The method returns the JavaType instance for the type name of the
   83        * specified class object. This is a convenience method for 
   84        * <code>getJavaType(clazz.getName())</code>. The major difference
   85        * between this method and getJavaType taking a type name is that this 
   86        * method is supposed to return a non-<code>null<code> value. The
   87        * specified class object describes an existing type.
   88        * @param clazz the Class instance representing the type
   89        * @return a JavaType instance for the name of the specified class
   90        * object.
   91        */
   92       public JavaType getJavaType(Class clazz)
   93       {
   94           return (clazz == null) ? null : getJavaType(clazz.getName());
   95       }
   96   
   97       /**
   98        * Finds a resource with a given name. A resource is some data that can
   99        * be accessed by class code in a way that is independent of the
  100        * location of the code. The name of a resource is a "/"-separated path
  101        * name that identifies the resource. The method method opens the
  102        * resource for reading and returns an InputStream. It returns 
  103        * <code>null</code> if no resource with this name is found or if the 
  104        * caller doesn't have adequate privileges to get the resource.
  105        * @param resourceName the resource name
  106        * @return an input stream for reading the resource, or <code>null</code> 
  107        * if the resource could not be found or if the caller doesn't have
  108        * adequate privileges to get the resource. 
  109        */
  110       abstract public InputStream getInputStreamForResource(String resourceName);
  111   
  112       /**
  113        * Returns the parent JavaModel instance of this JavaModel.
  114        * @return the parent JavaModel
  115        */
  116       public JavaModel getParent()
  117       {
  118           return parent;
  119       }
  120   
  121       /**
  122        * Set the parent JavaModel for this JavaModel. The method
  123        * automatically adds this JavaModel to the collection of children
  124        * of the specified parent JavaModel.
  125        * @param parent the parent JavaModel
  126        * @exception ModelException if impossible
  127        */
  128       public void setParent(JavaModel parent)
  129           throws ModelException
  130       {
  131           if (this.parent == parent) {
  132               // no changes => return;
  133               return;
  134           }
  135           if (this.parent != null) {
  136               // remove this from the collection of children of the old parent
  137               ((AbstractJavaModel)this.parent).children.remove(this);
  138               // add this to the collection of children of the new parent
  139               ((AbstractJavaModel)parent).children.add(this);
  140           }
  141           this.parent = parent;
  142       }
  143   
  144       /**
  145        * Returns a collection of child JavaModel instances in the form
  146        * of an array. All instances from the returned array have this
  147        * JavaModel instance as parent.
  148        * @return the child JavaModel instances
  149        */
  150       public JavaModel[] getChildren()
  151       {
  152           return (JavaModel[])children.toArray(new JavaModel[children.size()]);
  153       }
  154   
  155       /**
  156        * Returns the corresponding JDOModel instance.
  157        * @return the corresponding JDOModel.
  158        */
  159       public JDOModel getJDOModel()
  160       {
  161           return jdoModel;
  162       }
  163   
  164       /**
  165        * Sets the corresponding JDOModel instance.
  166        * @param jdoModel the JDOModel instance
  167        * @exception ModelException if impossible
  168        */
  169       public void setJDOModel(JDOModel jdoModel)
  170           throws ModelException
  171       {
  172           this.jdoModel = jdoModel;
  173       }
  174       
  175   }
  176   

Save This Page
Home » jdo2-model-2.3-ea-src » org.apache.jdo.impl.model.java » [javadoc | source]