You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2009/10/03 17:12:02 UTC
svn commit: r821336 - in
/commons/proper/configuration/branches/configuration2_experimental/src:
main/java/org/apache/commons/configuration2/base/Capability.java
test/java/org/apache/commons/configuration2/base/TestCapability.java
Author: oheger
Date: Sat Oct 3 15:12:02 2009
New Revision: 821336
URL: http://svn.apache.org/viewvc?rev=821336&view=rev
Log:
Added new Capability class.
Added:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java (with props)
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java (with props)
Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java?rev=821336&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java Sat Oct 3 15:12:02 2009
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.base;
+
+/**
+ * <p>
+ * A class that stores a capability object.
+ * </p>
+ * <p>
+ * A <em>capability</em> is a kind of extension of the
+ * {@link ConfigurationSource} interface that consists of a (interface) class
+ * and a corresponding implementation object. A client can request a capability
+ * of a specific type and gets the implementation object if it is available.
+ * </p>
+ * <p>
+ * This class stores the (interface) class and the implementation object. It
+ * provides a method to check whether it matches the request for a specific
+ * capability interface. An instance is fully initialized at construction time
+ * and immutable.
+ * </p>
+ *
+ * @author Commons Configuration team
+ * @version $Id$
+ */
+public class Capability
+{
+ /** The capability class. */
+ private final Class<?> capabilityClass;
+
+ /** The capability implementation object. */
+ private final Object capabilityObject;
+
+ /**
+ * Creates a new instance of {@code Capability} and initializes it.
+ *
+ * @param cls the class of this capability (must not be <b>null</b>)
+ * @param obj the implementation object (must not be <b>null</b>)
+ * @throws IllegalArgumentException if a required parameter is missing
+ */
+ public <T> Capability(Class<T> cls, T obj)
+ {
+ if (cls == null)
+ {
+ throw new IllegalArgumentException(
+ "Capability class must not be null!");
+ }
+ if (obj == null)
+ {
+ throw new IllegalArgumentException(
+ "Capability object must not be null!");
+ }
+
+ capabilityClass = cls;
+ capabilityObject = obj;
+ }
+
+ /**
+ * Returns the class of this capability.
+ *
+ * @return the capability class
+ */
+ public Class<?> getCapabilityClass()
+ {
+ return capabilityClass;
+ }
+
+ /**
+ * Returns the object implementing this capability.
+ *
+ * @return the implementation object
+ */
+ public Object getCapabilityObject()
+ {
+ return capabilityObject;
+ }
+
+ /**
+ * Returns a flag whether this capability matches the specified class. This
+ * means that the implementation object stored by this instance can be
+ * assigned to a variable of the given class.
+ *
+ * @param cls the requested class
+ * @return <b>true</b> if this capability matches this class, <b>false</b>
+ * otherwise
+ */
+ public boolean matches(Class<?> cls)
+ {
+ if (cls == null)
+ {
+ return false;
+ }
+
+ return cls.isAssignableFrom(getCapabilityClass());
+ }
+
+ /**
+ * Returns a string representation of this object. This string contains the
+ * capability class name and a string representation of the capability
+ * object.
+ *
+ * @return a string for this object
+ */
+ @Override
+ public String toString()
+ {
+ StringBuilder buf = new StringBuilder();
+ buf.append("Capability [ class = ");
+ buf.append(getCapabilityClass().getName());
+ buf.append(", object = ").append(getCapabilityObject());
+ buf.append(" ]");
+ return buf.toString();
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/Capability.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java?rev=821336&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java Sat Oct 3 15:12:02 2009
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.base;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@code Capability}.
+ *
+ * @author Commons Configuration team
+ * @version $Id$
+ */
+public class TestCapability extends TestCase
+{
+ /**
+ * Tries to create an instance without a class. This should cause an
+ * exception.
+ */
+ public void testInitNoClass()
+ {
+ try
+ {
+ new Capability(null, this);
+ fail("Could create an instance without a class!");
+ }
+ catch (IllegalArgumentException iex)
+ {
+ // ok
+ }
+ }
+
+ /**
+ * Tries to create an instance without an implementation object. This should
+ * cause an exception.
+ */
+ public void testInitNoObj()
+ {
+ try
+ {
+ new Capability(Object.class, null);
+ fail("Could create an instance without an instance!");
+ }
+ catch (IllegalArgumentException iex)
+ {
+ // ok
+ }
+ }
+
+ /**
+ * Tests whether an object can be created correctly.
+ */
+ public void testInit()
+ {
+ MapConfigurationSource src = new MapConfigurationSource();
+ Capability c = new Capability(ConfigurationSource.class, src);
+ assertEquals("Wrong class", ConfigurationSource.class, c
+ .getCapabilityClass());
+ assertSame("Wrong object", src, c.getCapabilityObject());
+ }
+
+ /**
+ * Tests the matches() method for an exact match.
+ */
+ public void testMatchesExact()
+ {
+ Capability c = new Capability(FlatConfigurationSource.class,
+ new MapConfigurationSource());
+ assertTrue("No match", c.matches(FlatConfigurationSource.class));
+ }
+
+ /**
+ * Tests the matches() method if the passed in class is a super class of the
+ * capability class.
+ */
+ public void testMatchesSuper()
+ {
+ Capability c = new Capability(FlatConfigurationSource.class,
+ new MapConfigurationSource());
+ assertTrue("No match", c.matches(ConfigurationSource.class));
+ }
+
+ /**
+ * Tests matches() if the class is not compatible.
+ */
+ public void testMatchesOther()
+ {
+ Capability c = new Capability(FlatConfigurationSource.class,
+ new MapConfigurationSource());
+ assertFalse("A match", c.matches(HierarchicalConfigurationSource.class));
+ }
+
+ /**
+ * Tests matches() for a null class.
+ */
+ public void testMatchesNull()
+ {
+ Capability c = new Capability(FlatConfigurationSource.class,
+ new MapConfigurationSource());
+ assertFalse("A match", c.matches(null));
+ }
+
+ /**
+ * Tests the string representation.
+ */
+ public void testToString()
+ {
+ MapConfigurationSource src = new MapConfigurationSource();
+ Capability c = new Capability(ConfigurationSource.class, src);
+ String s = c.toString();
+ assertEquals("Wrong string", String.format(
+ "Capability [ class = %s, object = %s ]",
+ ConfigurationSource.class.getName(), src.toString()), s);
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestCapability.java
------------------------------------------------------------------------------
svn:mime-type = text/plain