KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > enhanced > Personality


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: Personality.java,v 1.8 2005/02/21 12:03:40 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.enhanced;
8
9 import java.util.*;
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.util.CollectionFactory;
12
13 /**
14  * Defines a set of permitted mappings from [interface] Class objects to
15  * {@link Implementation} factories that can generate instances of the facet represented
16  * by the Class.
17  *
18  * @author <a HREF="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)<br>
19  * <a HREF="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)
20 */

21 public class Personality {
22
23     // Instance variables
24
/** Records the bindings from type specifications to implementations. */
25     private Map types = CollectionFactory.createHashedMap();
26     
27     // Constructors
28

29     /** base constructor, does nothing [except implicitly create _types_] */
30     protected Personality()
31         {}
32         
33     /** initialise this personality with the bindings from _other_ */
34     public Personality( Personality other )
35         {
36         this();
37         this.add( other );
38         }
39
40     // External contract methods
41

42     /** Add a new interface and its implementation to this Personality.
43         @param interf The interface to add, expressed as a Type object.
44         @param impl A way of implementing _interf_.
45      */

46     public Personality add( Class JavaDoc interf, Implementation impl )
47         {
48         types.put( interf, impl );
49         return this;
50         }
51
52     /**
53         create a new Personality copying this one; the _types_ state is
54         copied, not shared.
55     */

56     public Personality copy()
57         { return new Personality( this ); }
58     
59     /**
60         get the implemementation for the specified type, returning null if there
61         isn't one available.
62     */

63     Implementation getImplementation( Class JavaDoc t )
64         { return (Implementation) types.get( t ); }
65     
66     /**
67         extend this personality by adding in all the mappins from the argument _p_.
68         return _this_ (for call chaining).
69     */

70     Personality add( Personality p ) {
71         types.putAll( p.types );
72         return this;
73     }
74     
75     /**
76         make a new instance of a type _interf_ based on the node _n_ and the
77         polymorphic _that_; use the implementation wrapper for _interf_ in
78         _types_.
79     */

80     public Polymorphic newInstance(Class JavaDoc interf, Node n, Polymorphic that )
81         {
82         Implementation impl = (Implementation) types.get( interf );
83         if (impl == null) throw new PersonalityConfigException( interf + " not in Personality." );
84         Polymorphic rslt = impl.wrap( n, (EnhGraph) that );
85         if (!interf.isInstance(rslt))
86             throw new PersonalityConfigException( interf + " misconfigured." );
87
88         return rslt;
89         }
90     
91     protected Map getMap() {return types;}
92 }
93
94 /*
95     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
96     All rights reserved.
97
98     Redistribution and use in source and binary forms, with or without
99     modification, are permitted provided that the following conditions
100     are met:
101
102     1. Redistributions of source code must retain the above copyright
103        notice, this list of conditions and the following disclaimer.
104
105     2. Redistributions in binary form must reproduce the above copyright
106        notice, this list of conditions and the following disclaimer in the
107        documentation and/or other materials provided with the distribution.
108
109     3. The name of the author may not be used to endorse or promote products
110        derived from this software without specific prior written permission.
111
112     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
113     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
114     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
115     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
116     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
117     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
121     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122 */

123
Popular Tags