Java 类org.apache.commons.lang.math.NumberRange 实例源码

项目:seldon-server    文件:VariationTestingClientStrategyTest.java   
@Test
public void rangeTest()
{
    BigDecimal ratioTotal = new BigDecimal(1.0);
    BigDecimal currentMax = new BigDecimal(0.0);
    BigDecimal r1 = new BigDecimal(0.9);
    BigDecimal r2 = new BigDecimal(0.1);
    NumberRange range1 = new NumberRange(currentMax, currentMax.add(r1.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
    currentMax = currentMax.add(r1);
    NumberRange range2 = new NumberRange(currentMax.add(new BigDecimal(0.0001)), currentMax.add(r2.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
    BigDecimal t = new BigDecimal(0.900001);
    Assert.assertTrue(range1.containsNumber(t));
    Assert.assertFalse(range2.containsNumber(t));
    BigDecimal t2 = new BigDecimal(0.901);
    Assert.assertFalse(range1.containsNumber(t2));
    Assert.assertTrue(range2.containsNumber(t2));

}
项目:welshare    文件:ShortDomainInterceptor.java   
@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {

    if (request.getRequestURL().toString().contains(shortenerDomain)
            && !request.getRequestURI().startsWith(SHORT_URI_PREFIX)) {

        // if this is the root, redirect to the real domain
        if (request.getRequestURI().replace(request.getContextPath(), "").equals("/")) {
            response.sendRedirect(baseUrl);
            return false;
        }

        // if the length of the request URI is not a short key, redirect to base url

        String key = request.getRequestURI();

        // if ending in a punctuation mark, try stripping it before checking further
        // that's because some clients may not properly display urls followed by punctuation marks
        if (key.endsWith(")") || key.endsWith(".") || key.endsWith(",")) {
            key = key.substring(0, key.length() - 1);
        }

        if (new NumberRange(Constants.SHORT_URL_KEY_LENGTH,
                Constants.SHORT_URL_KEY_LENGTH + Constants.VIRAL_URL_ADDITION_LENGTH).containsInteger(key.length() - 1)) { //-1 for the leading slash
            request.getRequestDispatcher(SHORT_URI_PREFIX + request.getRequestURI())
                    .forward(request, response);
            return false;
        } else {
            response.sendRedirect(baseUrl);
            return false;
        }
    } else {
        return true;
    }
}
项目:ojb    文件:PersistenceBrokerTest.java   
/**
 * testing the sorted collections feature.)
 */
public void testSortedCollectionAttribute()
{
    String name = "testSortedCollectionAttribute_" + System.currentTimeMillis();
    ProductGroup samplePG = new ProductGroup();
    Article a1_ = createArticle(samplePG, name);
    Article a2_ = createArticle(samplePG, name);
    Article a3_ = createArticle(samplePG, name);
    // auto insert of referenced Article is enabled
    // and aX_ was added to PG
    broker.beginTransaction();
    broker.store(samplePG);
    broker.commitTransaction();
    broker.clearCache();

    InterfaceProductGroup pg = (InterfaceProductGroup) broker.getObjectByQuery(new QueryByIdentity(samplePG));
    List list = pg.getAllArticles();
    assertNotNull(list);
    assertEquals(3, list.size());
    NumberRange range = new NumberRange(a1_.getArticleId(), a3_.getArticleId());
    InterfaceArticle a1 = null;
    InterfaceArticle a2 = null;
    for(int i = 0; i < list.size(); i++)
    {
        a2 = a1;
        a1 =  (InterfaceArticle) list.get(i);
        if(i>0) assertTrue(a1.getArticleId().intValue() < a2.getArticleId().intValue());
        assertTrue(range.containsInteger(a1.getArticleId()));
    }
}
项目:ojb    文件:PersistenceBrokerTest.java   
public void testGetCollectionByQueryWithStartAndEnd() throws Exception
{
    String name = "testGetCollectionByQueryWithStartAndEnd_" + System.currentTimeMillis();
    Criteria criteria = new Criteria();
    criteria.addEqualTo("articleName", name);
    //            criteria.addEqualTo("isSelloutArticle", new Boolean(true));
    Query query = QueryFactory.newQuery(Article.class, criteria);
    Collection col = broker.getCollectionByQuery(query);
    assertEquals("size of collection should be zero", 0, col.size());
    //2. insert 5 matching items
    broker.beginTransaction();
    Article a1 = createArticle(null, name);
    broker.store(a1);
    Article a2 = createArticle(null, name);
    broker.store(a2);
    Article a3 = createArticle(null, name);
    broker.store(a3);
    Article a4 = createArticle(null, name);
    broker.store(a4);
    Article a5 = createArticle(null, name);
    broker.store(a5);
    broker.commitTransaction();

    broker.clearCache();
    // 3. set query start and end
    query.setStartAtIndex(2);
    query.setEndAtIndex(5);

    // 4. check if all items are found
    col = broker.getCollectionByQuery(query);
    assertEquals("size of collection should be four", 4, col.size());

    NumberRange range = new NumberRange(a1.getArticleId(), a5.getArticleId());
    Iterator iter = col.iterator();
    while (iter.hasNext())
    {
        InterfaceArticle testIa = (InterfaceArticle) iter.next();
        assertEquals("should be same value", name, testIa.getArticleName());
        Integer id = testIa.getArticleId();
        assertTrue("Id should be a number of the generated articles", range.containsInteger(id));
    }

    // read one item only
    // 1. set query start equals end
    query.setStartAtIndex(4);
    query.setEndAtIndex(4);

    // 2. check if only one item is found
    OJBIterator ojbIter = (OJBIterator)broker.getIteratorByQuery(query);
    assertEquals("size of iterator should be one", 1, ojbIter.size());
    InterfaceArticle test4 = (InterfaceArticle) ojbIter.next();
    ojbIter.releaseDbResources();
    assertTrue("Id should be a number of the generated articles", range.containsInteger(test4.getArticleId()));
}