/** * Copyright 2003, 2004, 2005. CodeStreet LLC. * * 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 com.codestreet.selector; import java.math.BigDecimal; import com.codestreet.selector.parser.IValueProvider; import com.codestreet.selector.parser.InvalidSelectorException; import com.codestreet.selector.parser.Result; import com.codestreet.selector.provider.bean.CachingValueProvider; /** * Created by IntelliJ IDEA. User: jawaid.hakim Date: Jan 26, 2009 Time: * 11:00:08 AM To change this template use Options | File Templates. */ public class Main { public Main() { } public static void main(String[] args) { try { String[] exprs = { ".stock.ticker = 'IBM' OR (.stock.shares > 100 AND .stock.shares < 500)", ".stock.ticker IN ('IBM', 'MSFT') OR (.stock.shares > 100 AND .stock.shares < 500)", ".stock.ticker IS NULL OR (.stock.shares > 100 AND .stock.shares < 500)" }; Trade[] beans = { new Trade(new Stock("IBM"), 750L, new BigDecimal(45.0)), new Trade(new Stock("MSFT"), 750L, new BigDecimal(45.0)), new Trade(new Stock(null), 200L, new BigDecimal(45.0)) }; // Create selector instance for (int i = 0; i < exprs.length; ++i) { ISelector sel = Selector.getInstance(exprs[i]); // Create value provider for bean IValueProvider provider = CachingValueProvider .valueOf(beans[i]); // Evaluate and print result Result res = sel.eval(provider, null); System.out.println("Result: " + res + ", " + exprs[i]); } } catch (InvalidSelectorException ex) { ex.printStackTrace(); } } public static class Trade { public Trade() { } public Trade(final Stock stock, final long shares, final BigDecimal price) { this.stock = stock; this.shares = shares; this.price = price; } public Stock getStock() { return stock; } public void setStock(final Stock stock) { this.stock = stock; } public long getShares() { return shares; } public void setShares(final long shares) { this.shares = shares; } public BigDecimal getPrice() { return price; } public void setPrice(final BigDecimal price) { this.price = price; } private Stock stock; private long shares; private BigDecimal price; } public static class Stock { public Stock() { } public Stock(final String ticker) { this.ticker = ticker; } public String getTicker() { return ticker; } public void setTicker(final String ticker) { this.ticker = ticker; } private String ticker; } }