• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

JAX-RS calculator  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to call the parse expression method in my JAX-RS using the @post method that accepts a string from a client.

below is the code i have the first bit of code only dose basic interger maths and the second bit of code does the parse expression but doesn't use JAX-RS

first code

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExpressionParser {

public static final Pattern SPLIT_REGEX = Pattern.compile("(?[-]?\\d+)([-+/*=])?)");

public long parseExpression(String expression) {
long left = 0;
char operator = '+';
Matcher m = SPLIT_REGEX.matcher(expression);
while (m.find()) {
String operand = "";
try {
operand = m.group(1);
} catch (IndexOutOfBoundsException e) {
operand = "0";
}
left = calculate(left, operator, Long.valueOf(operand));

String oper = m.group(2);
if (oper != null)
operator = oper.charAt(0);

}
return left;
}

private long calculate(long left, char operator, long right) {
switch (operator) {
case '+':
return left + right;
case '-':
return left - right;
case '*':
return left * right;
case '/':
return left / right;
case '=':
return left;
default:
return 0L;
}
}
}
second code

@Path("calculate")
public class Calculator {

   private final Long value;

   public Calculator() {
       value = 0L;
   }

   public Calculator(Long initialValue) {
       this.value = initialValue;
   }

   @GET
   public Long getValue() {
       return value;
   }

   @Path("add/{value1}/{value2}")
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   public Long add(@PathParam("value1") Long value, @PathParam("value2") Long value2) {
       return value + value2;
   }
   @Path("subtract/{value1}/{value2}")
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   public Long subtract(@PathParam("value1") Long value, @PathParam("value2") Long value2) {
       return value - value2;
   }
   @Path("multipli/{value1}/{value2}")
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   public Long multipli(@PathParam("value1") Long value, @PathParam("value2") Long value2) {
       return value * value2;
   }
   @Path("divide/{value1}/{value2}")
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   public Long divide(@PathParam("value1") Long value, @PathParam("value2") Long value2) {
       return value / value2;
   }
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!