hlship 2004/12/19 07:42:35
Added: framework/src/java/org/apache/hivemind/condtional Node.java
EvaluationContextImpl.java PropertyEvaulator.java
OrEvaluator.java NotEvaluator.java
ClassNameEvaluator.java Evaluator.java
EvaluationContext.java AndEvaluator.java
NodeImpl.java
Log:
Begin adding conditional contribution support.
Revision Changes Path
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/Node.java
Index: Node.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
/**
* An AST node in the tree parsed from the conditional expression. Nodes form
a binary tree, each
* node may have a left and a right sub-node.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public interface Node
{
public Node getLeft();
public Node getRight();
/**
* Evaluates the nodes using the context to provide access to runtime
information.
*/
public boolean evaluate(EvaluationContext context);
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/EvaluationContextImpl.java
Index: EvaluationContextImpl.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.Defense;
/**
* @author Howard M. Lewis Ship
*/
public class EvaluationContextImpl implements EvaluationContext
{
private ClassResolver _resolver;
public EvaluationContextImpl(ClassResolver resolver)
{
Defense.notNull(resolver, "resolver");
_resolver = resolver;
}
public boolean isPropertySet(String propertyName)
{
return Boolean.getBoolean(propertyName);
}
public boolean doesClassExist(String className)
{
try
{
_resolver.findClass(className);
return true;
}
catch (ApplicationRuntimeException ex)
{
return false;
}
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/PropertyEvaulator.java
Index: PropertyEvaulator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
import org.apache.hivemind.Defense;
/**
* Evaluates a system property and returns true if its value is true.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class PropertyEvaulator implements Evaluator
{
private String _propertyName;
public PropertyEvaulator(String propertyName)
{
Defense.notNull(propertyName, "propertyName");
_propertyName = propertyName;
}
/**
* Invokes [EMAIL PROTECTED]
org.apache.hivemind.condtional.EvaluationContext#isPropertySet(String)}.
*/
public boolean evaluate(EvaluationContext context, Node node)
{
return context.isPropertySet(_propertyName);
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/OrEvaluator.java
Index: OrEvaluator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
/**
* Or operation, returns true if either left or right Node evaluates to true.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class OrEvaluator implements Evaluator
{
public boolean evaluate(EvaluationContext context, Node node)
{
return node.getLeft().evaluate(context) ||
node.getRight().evaluate(context);
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/NotEvaluator.java
Index: NotEvaluator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
/**
* Evaluates the left child of its peer Node and inverts the result.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class NotEvaluator implements Evaluator
{
public boolean evaluate(EvaluationContext context, Node node)
{
return !node.getLeft().evaluate(context);
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/ClassNameEvaluator.java
Index: ClassNameEvaluator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
import org.apache.hivemind.Defense;
/**
* Evaluator for a fully qualified class name.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class ClassNameEvaluator implements Evaluator
{
private String _className;
public ClassNameEvaluator(String className)
{
Defense.notNull(className, "className");
_className = className;
}
public boolean evaluate(EvaluationContext context, Node node)
{
return context.doesClassExist(_className);
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/Evaluator.java
Index: Evaluator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
/**
* An evaluator is paired with a [EMAIL PROTECTED] Node}. The Node provides
structure, the Evaluator provides
* meaning, interpreting the node.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public interface Evaluator
{
/**
* Invoked by the Node to evaluate its own value. Typical implementations
will extract the
* Node's [EMAIL PROTECTED] org.apache.hivemind.condtional.Node#getLeft()
left} and
* [EMAIL PROTECTED] org.apache.hivemind.condtional.Node#getRight()
right}  properties and combine or
* otherwise evaluate them. Terminal nodes in the tree will have
evaluators that don't do that
* but generate a return value internally.
*/
public boolean evaluate(EvaluationContext context, Node node);
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/EvaluationContext.java
Index: EvaluationContext.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
/**
* Provides context when evaluating an AST of [EMAIL PROTECTED]
org.apache.hivemind.condtional.Node}s.
* Effectively, a wrapper around certain runtime operations.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public interface EvaluationContext
{
/**
* Returns true if the given system property is set.
*/
public boolean isPropertySet(String propertyName);
/**
* Returns true if the class, specified by FQCN, exists.
*
*/
public boolean doesClassExist(String className);
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/AndEvaluator.java
Index: AndEvaluator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
/**
* And operation, evaluates the left node and then (perhaps) the right node
(short circuiting may
* take place).
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class AndEvaluator implements Evaluator
{
public boolean evaluate(EvaluationContext context, Node node)
{
return node.getLeft().evaluate(context) &&
node.getRight().evaluate(context);
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/condtional/NodeImpl.java
Index: NodeImpl.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed 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.hivemind.condtional;
import org.apache.hivemind.Defense;
/**
* Implementation of [EMAIL PROTECTED] org.apache.hivemind.condtional.Node}.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class NodeImpl implements Node
{
private Node _left;
private Node _right;
private Evaluator _evaluator;
public NodeImpl(Node left, Node right, Evaluator evaluator)
{
Defense.notNull(evaluator, "evaluator");
_left = left;
_right = right;
_evaluator = evaluator;
}
/**
* Alternate constructor used for terminal nodes.
*/
public NodeImpl(Evaluator evaluator)
{
this(null, null, evaluator);
}
public Node getLeft()
{
return _left;
}
public Node getRight()
{
return _right;
}
public boolean evaluate(EvaluationContext context)
{
return _evaluator.evaluate(context, this);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]