001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: Personality.java,v 1.11 2008/01/02 12:09:12 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.enhanced;
008:
009: import java.util.*;
010: import com.hp.hpl.jena.graph.*;
011: import com.hp.hpl.jena.util.CollectionFactory;
012:
013: /**
014: * Defines a set of permitted mappings from [interface] Class objects to
015: * {@link Implementation} factories that can generate instances of the facet represented
016: * by the Class.
017: *
018: * @author <a href="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)<br>
019: * <a href="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)
020: */
021: public class Personality {
022:
023: // Instance variables
024: /** Records the bindings from type specifications to implementations. */
025: private Map types = CollectionFactory.createHashedMap();
026:
027: // Constructors
028:
029: /** base constructor, does nothing [except implicitly create _types_] */
030: protected Personality() {
031: }
032:
033: /** initialise this personality with the bindings from _other_ */
034: public Personality(Personality other) {
035: this ();
036: this .add(other);
037: }
038:
039: // External contract methods
040:
041: /** Add a new interface and its implementation to this Personality.
042: @param interf The interface to add, expressed as a Type object.
043: @param impl A way of implementing _interf_.
044: */
045: public Personality add(Class interf, Implementation impl) {
046: types.put(interf, impl);
047: return this ;
048: }
049:
050: /**
051: create a new Personality copying this one; the _types_ state is
052: copied, not shared.
053: */
054: public Personality copy() {
055: return new Personality(this );
056: }
057:
058: /**
059: get the implemementation for the specified type, returning null if there
060: isn't one available.
061: */
062: Implementation getImplementation(Class t) {
063: return (Implementation) types.get(t);
064: }
065:
066: /**
067: extend this personality by adding in all the mappins from the argument _p_.
068: return _this_ (for call chaining).
069: */
070: Personality add(Personality p) {
071: types.putAll(p.types);
072: return this ;
073: }
074:
075: /**
076: make a new instance of a type _interf_ based on the node _n_ and the
077: polymorphic _that_; use the implementation wrapper for _interf_ in
078: _types_.
079: */
080: public Polymorphic newInstance(Class interf, Node n,
081: Polymorphic that) {
082: Implementation impl = (Implementation) types.get(interf);
083: if (impl == null)
084: throw new PersonalityConfigException(interf
085: + " not in Personality.");
086: Polymorphic rslt = impl.wrap(n, (EnhGraph) that);
087: if (!interf.isInstance(rslt))
088: throw new PersonalityConfigException(interf
089: + " misconfigured.");
090:
091: return rslt;
092: }
093:
094: protected Map getMap() {
095: return types;
096: }
097: }
098:
099: /*
100: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
101: All rights reserved.
102:
103: Redistribution and use in source and binary forms, with or without
104: modification, are permitted provided that the following conditions
105: are met:
106:
107: 1. Redistributions of source code must retain the above copyright
108: notice, this list of conditions and the following disclaimer.
109:
110: 2. Redistributions in binary form must reproduce the above copyright
111: notice, this list of conditions and the following disclaimer in the
112: documentation and/or other materials provided with the distribution.
113:
114: 3. The name of the author may not be used to endorse or promote products
115: derived from this software without specific prior written permission.
116:
117: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
118: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
119: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
120: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
121: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
122: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
123: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
124: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
125: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
126: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
127: */
|