dflorey 2004/10/08 03:44:57
Added: contract/src/examples/org/apache/commons/contract/example
SpeedCalculator.java SimpleMain.java
Removed: contract/src/java/org/apache/commons/contract/example
SpeedCalculator.java SimpleMain.java
Log:
Moven examples from main jar to examples jar
Revision Changes Path
1.1
jakarta-commons-sandbox/contract/src/examples/org/apache/commons/contract/example/SpeedCalculator.java
Index: SpeedCalculator.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.commons.contract.example;
import java.util.Map;
import org.apache.commons.contract.Context;
import org.apache.commons.contract.Processor;
import org.apache.commons.contract.Result;
import org.apache.commons.contract.constraints.NumberConstraints;
import org.apache.commons.contract.constraints.StringConstraints;
import org.apache.commons.contract.descriptor.ParameterDescriptor;
import org.apache.commons.contract.descriptor.ResultDescriptor;
import org.apache.commons.contract.descriptor.ResultEntryDescriptor;
import org.apache.commons.contract.descriptor.StateDescriptor;
import org.apache.commons.contract.i18n.ParameterMessage;
import org.apache.commons.i18n.LocalizedMessage;
public class SpeedCalculator implements Processor {
public final static String SPEED = "speed";
private final static String DISTANCE = "distance";
private final static String TIME = "time";
private final static String UNIT = "unit";
private final static String SECONDS = "s";
private final static String MINUTES = "min";
private final static String HOURS = "h";
ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[]{
new ParameterDescriptor(DISTANCE, new
ParameterMessage("computeSpeed/parameter/distance"),
new NumberConstraints(
new Integer(0), null, true)),
new ParameterDescriptor(UNIT, new
ParameterMessage("computeSpeed/parameter/unit"),
new StringConstraints(
new String[]{SECONDS, MINUTES, HOURS})),
new ParameterDescriptor(TIME, new
ParameterMessage("computeSpeed/parameter/time"),
NumberConstraints.POSITIVE)};
ResultDescriptor[] resultDescriptors = new ResultDescriptor[]{new
ResultDescriptor(
StateDescriptor.OK_DESCRIPTOR,
new ResultEntryDescriptor[]{new ResultEntryDescriptor(SPEED,
new LocalizedMessage("computeSpeed/result/speed"),
new NumberConstraints(new Float(0.1), new
Integer(Integer.MAX_VALUE)))})};
/**
* Computes the speed in meter per second from the given distance and time.
* The time can be given in seconds, minutes or hours, depending on the
* content of timeUnit.
*/
public Result process(Map parameters, Context context) {
float distance = ((Number)parameters.get(DISTANCE)).floatValue();
float time = ((Number)parameters.get(TIME)).floatValue();
String timeUnit = (String)parameters.get(UNIT);
float speed;
if (timeUnit.equals("s")) speed = distance / time;
else if (timeUnit.equals("min")) speed = distance*60 / time;
else speed = distance*3600 / time;
return new Result(StateDescriptor.OK, SPEED, new Float(speed));
}
public ParameterDescriptor[] getParameterDescriptors() {
return parameterDescriptors;
}
public ResultDescriptor[] getResultDescriptors() {
return resultDescriptors;
}
}
1.1
jakarta-commons-sandbox/contract/src/examples/org/apache/commons/contract/example/SimpleMain.java
Index: SimpleMain.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.commons.contract.example;
import org.apache.commons.contract.Result;
import org.apache.commons.contract.util.InteractiveMainWrapper;
import org.apache.commons.contract.util.MainWrapper;
import org.apache.commons.i18n.MessageManager;
public class SimpleMain {
public static void main(String[] args) {
MessageManager.install("contract/example",
Thread.currentThread().getContextClassLoader().getResourceAsStream("example.xml"));
Result result = InteractiveMainWrapper.main(args, new SpeedCalculator());
if ( result != null ) {
System.out.println("Speed:
"+result.getResultEntries().get(SpeedCalculator.SPEED));
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]