From: <be...@us...> - 2010-03-26 21:24:58
|
Revision: 316 http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=316&view=rev Author: benoitx Date: 2010-03-26 21:24:51 +0000 (Fri, 26 Mar 2010) Log Message: ----------- Tidy up and adding a skeleton for test. Modified Paths: -------------- trunk/utils/pom.xml trunk/utils/src/main/java/net/objectlab/kit/console/ConsoleMenu.java trunk/utils/src/main/java/net/objectlab/kit/console/Repeater.java trunk/utils/src/main/java/net/objectlab/kit/util/Average.java trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/BooleanUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/CaseTreatment.java trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/IntegerUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/ObjectHolder.java trunk/utils/src/main/java/net/objectlab/kit/util/ObjectUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/Pair.java trunk/utils/src/main/java/net/objectlab/kit/util/Quadruplet.java trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java trunk/utils/src/main/java/net/objectlab/kit/util/WeightedAverage.java Added Paths: ----------- trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java Modified: trunk/utils/pom.xml =================================================================== --- trunk/utils/pom.xml 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/pom.xml 2010-03-26 21:24:51 UTC (rev 316) @@ -21,6 +21,12 @@ <artifactId>joda-time</artifactId> <version>1.6</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.1</version> + <scope>test</scope> + </dependency> </dependencies> <build> Modified: trunk/utils/src/main/java/net/objectlab/kit/console/ConsoleMenu.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/console/ConsoleMenu.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/console/ConsoleMenu.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.console; import java.io.BufferedReader; @@ -206,8 +238,8 @@ * @return boolean as selected by the user of the console app */ public static boolean getBoolean(final String title, final boolean defaultValue) { - final String val = ConsoleMenu.selectOne(title, new String[] { "Yes", "No" }, new String[] { Boolean.TRUE.toString(), - Boolean.FALSE.toString() }, defaultValue ? 1 : 2); + final String val = ConsoleMenu.selectOne(title, new String[] { "Yes", "No" }, new String[] { Boolean.TRUE.toString(), Boolean.FALSE.toString() }, + defaultValue ? 1 : 2); return Boolean.valueOf(val); } Modified: trunk/utils/src/main/java/net/objectlab/kit/console/Repeater.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/console/Repeater.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/console/Repeater.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.console; import java.lang.reflect.Method; Modified: trunk/utils/src/main/java/net/objectlab/kit/util/Average.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/Average.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/Average.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.math.BigDecimal; @@ -18,11 +50,11 @@ } public Average(final int scale) { - BigDecimal bd = new BigDecimal(0); + final BigDecimal bd = new BigDecimal(0); sum = new Sum(bd.setScale(scale)); } - public void add(BigDecimal val) { + public void add(final BigDecimal val) { sum.add(val); count++; } Modified: trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.math.BigDecimal; @@ -113,8 +145,10 @@ */ public static BigDecimal subtract(final BigDecimal start, final BigDecimal... values) { BigDecimal total = start; - for (final BigDecimal v : values) { - total = subtract(total, v); + if (values != null) { + for (final BigDecimal v : values) { + total = subtract(total, v); + } } return total; } @@ -144,8 +178,8 @@ } public static BigDecimal calculateWeight(final BigDecimal value, final BigDecimal total) { - return BigDecimalUtil.setScale(BigDecimalUtil.divide(BigDecimalUtil.setScale(value, 9), BigDecimalUtil.setScale(total, 9), - BigDecimal.ROUND_HALF_UP), 9); + return BigDecimalUtil + .setScale(BigDecimalUtil.divide(BigDecimalUtil.setScale(value, 9), BigDecimalUtil.setScale(total, 9), BigDecimal.ROUND_HALF_UP), 9); } /** @@ -388,8 +422,8 @@ */ public static BigDecimal addWeightedConstituent(final BigDecimal runningWeightedVal, final BigDecimal valueToAdd, final BigDecimal weightForValueToAdd, final BigDecimal totalWeight) { - return BigDecimalUtil.add(runningWeightedVal, BigDecimalUtil.divide(BigDecimalUtil - .multiply(valueToAdd, BigDecimalUtil.abs(weightForValueToAdd)), BigDecimalUtil.abs(totalWeight), BigDecimal.ROUND_HALF_UP)); + return BigDecimalUtil.add(runningWeightedVal, BigDecimalUtil.divide(BigDecimalUtil.multiply(valueToAdd, BigDecimalUtil.abs(weightForValueToAdd)), + BigDecimalUtil.abs(totalWeight), BigDecimal.ROUND_HALF_UP)); } /** @@ -611,7 +645,7 @@ return null; } BigDecimal max = null; - for (BigDecimal bd : v1) { + for (final BigDecimal bd : v1) { max = BigDecimalUtil.compareTo(max, bd) >= 0 ? max : bd; } return max; @@ -643,7 +677,7 @@ /** * @return true if value !=null and <=0. */ - public static boolean isZeroOrLess(BigDecimal value) { + public static boolean isZeroOrLess(final BigDecimal value) { return value != null && value.signum() <= 0; } } Modified: trunk/utils/src/main/java/net/objectlab/kit/util/BooleanUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/BooleanUtil.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/BooleanUtil.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; public final class BooleanUtil { Modified: trunk/utils/src/main/java/net/objectlab/kit/util/CaseTreatment.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/CaseTreatment.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/CaseTreatment.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; public enum CaseTreatment { Modified: trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.util.Collection; @@ -57,8 +89,8 @@ */ public static boolean containsAny(final Collection<?> collection, final Object... items) { if (collection != null) { - for (Object item : items) { - boolean b = collection.contains(item); + for (final Object item : items) { + final boolean b = collection.contains(item); if (b) { return true; } Modified: trunk/utils/src/main/java/net/objectlab/kit/util/IntegerUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/IntegerUtil.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/IntegerUtil.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; /** Modified: trunk/utils/src/main/java/net/objectlab/kit/util/ObjectHolder.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/ObjectHolder.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/ObjectHolder.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; /** Modified: trunk/utils/src/main/java/net/objectlab/kit/util/ObjectUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/ObjectUtil.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/ObjectUtil.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.math.BigDecimal; Modified: trunk/utils/src/main/java/net/objectlab/kit/util/Pair.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/Pair.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/Pair.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.io.Serializable; Modified: trunk/utils/src/main/java/net/objectlab/kit/util/Quadruplet.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/Quadruplet.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/Quadruplet.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.io.Serializable; Modified: trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.text.SimpleDateFormat; Modified: trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java 2010-03-26 21:24:51 UTC (rev 316) @@ -42,6 +42,10 @@ */ public class Sum implements Serializable { + /** + * + */ + private static final long serialVersionUID = 1L; private BigDecimal value = BigDecimal.ZERO; private int count = 0; @@ -55,7 +59,7 @@ } } - public Sum(final BigDecimal start, int scale) { + public Sum(final BigDecimal start, final int scale) { if (start != null) { value = start.setScale(scale, BigDecimal.ROUND_HALF_UP); } @@ -87,7 +91,7 @@ } public Sum subtract(final BigDecimal... value) { - this.value = BigDecimalUtil.subtract(this.value, value); + this.value = BigDecimalUtil.subtract(this.value, value); count += value.length; return this; } Modified: trunk/utils/src/main/java/net/objectlab/kit/util/WeightedAverage.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/WeightedAverage.java 2010-03-26 21:09:51 UTC (rev 315) +++ trunk/utils/src/main/java/net/objectlab/kit/util/WeightedAverage.java 2010-03-26 21:24:51 UTC (rev 316) @@ -1,3 +1,35 @@ +/* + * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit. + * + * Based in London, we are world leaders in the design and development + * of bespoke applications for the securities financing markets. + * + * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> + * ___ _ _ _ _ _ + * / _ \| |__ (_) ___ ___| |_| | __ _| |__ + * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ + * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | + * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ + * |__/ + * + * www.ObjectLab.co.uk + * + * $Id: AbstractDateCalculator.java 309 2010-03-23 21:01:49Z marchy $ + * + * Copyright 2006 the original author or authors. + * + * 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 net.objectlab.kit.util; import java.io.Serializable; Added: trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java (rev 0) +++ trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java 2010-03-26 21:24:51 UTC (rev 316) @@ -0,0 +1,491 @@ +/** + * + */ +package net.objectlab.kit.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.math.BigDecimal; + +import org.junit.Test; + +/** + * @author Benoit + * + */ +public class BigDecimalUtilTest { + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#inverse(java.math.BigDecimal, int)}. + */ + @Test + public void testInverseBigDecimalInt() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#inverse(java.math.BigDecimal)}. + */ + @Test + public void testInverseBigDecimal() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isNotZero(java.math.BigDecimal)}. + */ + @Test + public void testIsNotZero() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isZero(java.math.BigDecimal)}. + */ + @Test + public void testIsZero() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isNegative(java.math.BigDecimal)}. + */ + @Test + public void testIsNegative() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isStrictlyPositive(java.math.BigDecimal)}. + */ + @Test + public void testIsStrictlyPositive() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isNullOrZero(java.math.BigDecimal)}. + */ + @Test + public void testIsNullOrZero() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isSameValue(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsSameValue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isSameValueTreatNullAsZero(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsSameValueTreatNullAsZero() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#add(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testAddBigDecimalBigDecimal() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#add(java.math.BigDecimal, java.math.BigDecimal[])}. + */ + @Test + public void testAddBigDecimalBigDecimalArray() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#subtract(java.math.BigDecimal, java.math.BigDecimal[])}. + */ + @Test + public void testSubtractBigDecimalBigDecimalArray() { + assertEquals("2 null", BigDecimal.ZERO, BigDecimalUtil.subtract(null, (BigDecimal[]) null)); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#subtract(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testSubtractBigDecimalBigDecimal() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#divide(java.math.BigDecimal, java.math.BigDecimal, int)}. + */ + @Test + public void testDivideBigDecimalBigDecimalInt() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#calculateWeight(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testCalculateWeight() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#divide(int, java.math.BigDecimal, java.math.BigDecimal, int)}. + */ + @Test + public void testDivideIntBigDecimalBigDecimalInt() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#divide(java.math.BigDecimal, java.math.BigDecimal, int, int)}. + */ + @Test + public void testDivideBigDecimalBigDecimalIntInt() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#multiply(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testMultiplyBigDecimalBigDecimal() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#multiply(java.math.BigDecimal, java.math.BigDecimal[])}. + */ + @Test + public void testMultiplyBigDecimalBigDecimalArray() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#abs(java.math.BigDecimal)}. + */ + @Test + public void testAbs() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#negate(java.math.BigDecimal)}. + */ + @Test + public void testNegate() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#negateIfTrue(boolean, java.math.BigDecimal)}. + */ + @Test + public void testNegateIfTrue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isNotSameAbsValue(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsNotSameAbsValue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isNotSameValue(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsNotSameValue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isSameAbsValue(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsSameAbsValue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#compareTo(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testCompareTo() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#absCompareTo(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testAbsCompareTo() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#absDiff(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testAbsDiff() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#movePoint(java.math.BigDecimal, int)}. + */ + @Test + public void testMovePoint() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#roundTo(java.math.BigDecimal, int, int)}. + */ + @Test + public void testRoundTo() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#setScale(java.math.BigDecimal, int)}. + */ + @Test + public void testSetScaleBigDecimalInt() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#setScale(java.math.BigDecimal, java.lang.Integer)}. + */ + @Test + public void testSetScaleBigDecimalInteger() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#setScale(java.math.BigDecimal, java.lang.Integer, int)}. + */ + @Test + public void testSetScaleBigDecimalIntegerInt() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#signum(java.math.BigDecimal)}. + */ + @Test + public void testSignum() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isSameSignum(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsSameSignum() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#hasSignedFlippedAndNotZero(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testHasSignedFlippedAndNotZero() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#hasSignedChanged(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testHasSignedChanged() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isOutsideRange(java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsOutsideRange() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isInsideInclusiveRange(java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsInsideInclusiveRange() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#assignNonNull(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testAssignNonNull() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#addWeightedConstituent(java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testAddWeightedConstituent() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#allNullOrZero(java.math.BigDecimal[])}. + */ + @Test + public void testAllNullOrZero() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#format(java.math.BigDecimal)}. + */ + @Test + public void testFormat() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#percentFormat(java.math.BigDecimal)}. + */ + @Test + public void testPercentFormat() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#movedInsideThresholdPercentage(java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testMovedInsideThresholdPercentage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#movedStrictlyOutsideThresholdPercentage(java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testMovedStrictlyOutsideThresholdPercentage() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#roundUp(java.math.BigDecimal, int)}. + */ + @Test + public void testRoundUp() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#roundDown(java.math.BigDecimal, int)}. + */ + @Test + public void testRoundDown() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#roundUpForIncrement(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testRoundUpForIncrement() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#roundDownForIncrement(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testRoundDownForIncrement() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#ensureMin(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testEnsureMin() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#forceNegative(java.math.BigDecimal)}. + */ + @Test + public void testForceNegative() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#forceNegativeIfTrue(boolean, java.math.BigDecimal)}. + */ + @Test + public void testForceNegativeIfTrue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#min(java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testMin() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#max(java.math.BigDecimal[])}. + */ + @Test + public void testMax() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#longForFraction(java.math.BigDecimal)}. + */ + @Test + public void testLongForFraction() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isDiffMoreThanAbsThreshold(java.math.BigDecimal, java.math.BigDecimal, java.math.BigDecimal)}. + */ + @Test + public void testIsDiffMoreThanAbsThreshold() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#doubleValue(java.math.BigDecimal)}. + */ + @Test + public void testDoubleValue() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#isZeroOrLess(java.math.BigDecimal)}. + */ + @Test + public void testIsZeroOrLess() { + fail("Not yet implemented"); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2010-03-28 10:55:15
|
Revision: 318 http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=318&view=rev Author: benoitx Date: 2010-03-28 10:55:09 +0000 (Sun, 28 Mar 2010) Log Message: ----------- Adding tests. Modified Paths: -------------- trunk/utils/pom.xml trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java Modified: trunk/utils/pom.xml =================================================================== --- trunk/utils/pom.xml 2010-03-26 21:32:38 UTC (rev 317) +++ trunk/utils/pom.xml 2010-03-28 10:55:09 UTC (rev 318) @@ -3,8 +3,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> - <groupId>net.objectlab.kit.util</groupId> - <artifactId>utils</artifactId> + <groupId>net.objectlab.kit</groupId> + <artifactId>objectlab-utils</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> Modified: trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java 2010-03-26 21:32:38 UTC (rev 317) +++ trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java 2010-03-28 10:55:09 UTC (rev 318) @@ -119,7 +119,7 @@ /** * Add 2 BigDecimal safely (i.e. handles nulls) */ - public static BigDecimal add(final BigDecimal v1, final BigDecimal v2) { + private static BigDecimal doAdd(final BigDecimal v1, final BigDecimal v2) { BigDecimal total = v1; if (v1 != null && v2 != null) { total = v1.add(v2); @@ -133,21 +133,23 @@ * Add n BigDecimal safely (i.e. handles nulls) */ public static BigDecimal add(final BigDecimal start, final BigDecimal... values) { - BigDecimal total = start; - for (final BigDecimal v : values) { - total = add(total, v); + BigDecimal total = start != null ? start : BigDecimal.ZERO; + if (values != null) { + for (final BigDecimal v : values) { + total = doAdd(total, v); + } } return total; } /** - * Subtract n BigDecimal safely (i.e. handles nulls) + * Subtract n BigDecimal safely (i.e. handles nulls), returns 0 */ public static BigDecimal subtract(final BigDecimal start, final BigDecimal... values) { - BigDecimal total = start; + BigDecimal total = start != null ? start : BigDecimal.ZERO; if (values != null) { for (final BigDecimal v : values) { - total = subtract(total, v); + total = doSubtract(total, v); } } return total; @@ -156,7 +158,7 @@ /** * Subtract 2 BigDecimal safely (i.e. handles nulls) v1 - v2 */ - public static BigDecimal subtract(final BigDecimal v1, final BigDecimal v2) { + private static BigDecimal doSubtract(final BigDecimal v1, final BigDecimal v2) { BigDecimal diff = v1; if (v1 != null && v2 != null) { diff = v1.subtract(v2); @@ -295,7 +297,7 @@ * @return true if the ABS( ABS(v1) - ABS(v2) ) */ public static BigDecimal absDiff(final BigDecimal v1, final BigDecimal v2) { - return abs(subtract(abs(v1), abs(v2))); + return abs(doSubtract(abs(v1), abs(v2))); } /** @@ -422,7 +424,7 @@ */ public static BigDecimal addWeightedConstituent(final BigDecimal runningWeightedVal, final BigDecimal valueToAdd, final BigDecimal weightForValueToAdd, final BigDecimal totalWeight) { - return BigDecimalUtil.add(runningWeightedVal, BigDecimalUtil.divide(BigDecimalUtil.multiply(valueToAdd, BigDecimalUtil.abs(weightForValueToAdd)), + return BigDecimalUtil.doAdd(runningWeightedVal, BigDecimalUtil.divide(BigDecimalUtil.multiply(valueToAdd, BigDecimalUtil.abs(weightForValueToAdd)), BigDecimalUtil.abs(totalWeight), BigDecimal.ROUND_HALF_UP)); } @@ -475,7 +477,7 @@ public static boolean movedStrictlyOutsideThresholdPercentage(final BigDecimal startValue, final BigDecimal newValue, final BigDecimal thresholdPercent) { final BigDecimal s = BigDecimalUtil.setScale(startValue, 10); final BigDecimal n = BigDecimalUtil.setScale(newValue, 10); - final BigDecimal diff = BigDecimalUtil.divide(BigDecimalUtil.subtract(s, n), s, BigDecimal.ROUND_HALF_UP); + final BigDecimal diff = BigDecimalUtil.divide(BigDecimalUtil.doSubtract(s, n), s, BigDecimal.ROUND_HALF_UP); // diff = BigDecimalUtil.safeAbsDiff(diff, MagicNumbers.ONE); return BigDecimalUtil.absCompareTo(diff, thresholdPercent) > 0; Modified: trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java 2010-03-26 21:32:38 UTC (rev 317) +++ trunk/utils/src/test/java/net/objectlab/kit/util/BigDecimalUtilTest.java 2010-03-28 10:55:09 UTC (rev 318) @@ -17,6 +17,34 @@ public class BigDecimalUtilTest { /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#divide(java.math.BigDecimal, java.math.BigDecimal, int, int)}. + */ + @Test + public void testDivideBigDecimalBigDecimalIntInt() { + assertEquals("3 null", null, BigDecimalUtil.divide(null, null, BigDecimal.ROUND_HALF_UP)); + assertEquals("2a null", null, BigDecimalUtil.divide(BigDecimal.ONE, null, BigDecimal.ROUND_HALF_UP)); + assertEquals("2c null", null, BigDecimalUtil.divide(null, BigDecimal.ONE, BigDecimal.ROUND_HALF_UP)); + assertEquals("2d null", null, BigDecimalUtil.divide(null, BigDecimal.ONE, BigDecimal.ROUND_HALF_UP)); + assertEquals("ONE 2", BigDecimal.ONE, BigDecimalUtil.divide(BigDecimal.ONE, BigDecimal.ONE, BigDecimal.ROUND_HALF_UP)); + assertEquals("ONE 3", BigDecimal.ONE, BigDecimalUtil.divide(BigDecimal.ONE, BigDecimal.ONE, BigDecimal.ROUND_HALF_UP)); + } + + /** + * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#setScale(java.math.BigDecimal, int)}. + */ + @Test + public void testSetScaleBigDecimalInt() { + assertEquals("null", null, BigDecimalUtil.setScale(null, 3)); + assertEquals("1.000", new BigDecimal("1.000"), BigDecimalUtil.setScale(BigDecimal.ONE, 3)); + assertEquals("1.000 same", new BigDecimal("1.000"), BigDecimalUtil.setScale(new BigDecimal("1.000"), 3)); + assertEquals("1.00 to 3dp", new BigDecimal("1.000"), BigDecimalUtil.setScale(new BigDecimal("1.00"), 3)); + assertEquals("1.000 to -1dp", new BigDecimal("0E+1"), BigDecimalUtil.setScale(new BigDecimal("1.000"), -1)); + assertEquals("1.236 to 2", new BigDecimal("1.24"), BigDecimalUtil.setScale(new BigDecimal("1.236"), 2)); + assertEquals("1.235 to 2", new BigDecimal("1.24"), BigDecimalUtil.setScale(new BigDecimal("1.235"), 2)); + assertEquals("1.234 to 2", new BigDecimal("1.23"), BigDecimalUtil.setScale(new BigDecimal("1.23"), 2)); + } + + /** * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#inverse(java.math.BigDecimal, int)}. */ @Test @@ -89,19 +117,19 @@ } /** - * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#add(java.math.BigDecimal, java.math.BigDecimal)}. - */ - @Test - public void testAddBigDecimalBigDecimal() { - fail("Not yet implemented"); - } - - /** * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#add(java.math.BigDecimal, java.math.BigDecimal[])}. */ @Test public void testAddBigDecimalBigDecimalArray() { - fail("Not yet implemented"); + assertEquals("null", BigDecimal.ZERO, BigDecimalUtil.add(null)); + assertEquals("null+null", BigDecimal.ZERO, BigDecimalUtil.add(null, null)); + assertEquals("null+null+0", BigDecimal.ZERO, BigDecimalUtil.add(null, null, BigDecimal.ZERO)); + assertEquals("null+1", BigDecimal.ONE, BigDecimalUtil.add(null, BigDecimal.ONE)); + assertEquals("1+null", BigDecimal.ONE, BigDecimalUtil.add(BigDecimal.ONE, null)); + assertEquals("1+null+0", BigDecimal.ONE, BigDecimalUtil.add(BigDecimal.ONE, null, BigDecimal.ZERO)); + assertEquals("1+null+1", new BigDecimal(2), BigDecimalUtil.add(BigDecimal.ONE, null, BigDecimal.ONE)); + assertEquals("1+0+1", new BigDecimal(2), BigDecimalUtil.add(BigDecimal.ONE, BigDecimal.ZERO, BigDecimal.ONE)); + assertEquals("1+1+1", new BigDecimal(3), BigDecimalUtil.add(BigDecimal.ONE, BigDecimal.ONE, BigDecimal.ONE)); } /** @@ -110,17 +138,17 @@ @Test public void testSubtractBigDecimalBigDecimalArray() { assertEquals("2 null", BigDecimal.ZERO, BigDecimalUtil.subtract(null, (BigDecimal[]) null)); + assertEquals("3 null", BigDecimal.ZERO, BigDecimalUtil.subtract(null, null, null)); + assertEquals("0 -null -null", BigDecimal.ZERO, BigDecimalUtil.subtract(BigDecimal.ZERO, null, null)); + assertEquals("null 0 -null", BigDecimal.ZERO, BigDecimalUtil.subtract(null, BigDecimal.ZERO, null)); + assertEquals("1 -null", BigDecimal.ONE, BigDecimalUtil.subtract(BigDecimal.ONE, null)); + assertEquals("1 -null -0", BigDecimal.ONE, BigDecimalUtil.subtract(BigDecimal.ONE, null, BigDecimal.ZERO)); + assertEquals("1 -0 -null", BigDecimal.ONE, BigDecimalUtil.subtract(BigDecimal.ONE, BigDecimal.ZERO, null)); + assertEquals("1 -0 -1", BigDecimal.ZERO, BigDecimalUtil.subtract(BigDecimal.ONE, BigDecimal.ZERO, BigDecimal.ONE)); + assertEquals("1 -null -1", BigDecimal.ZERO, BigDecimalUtil.subtract(BigDecimal.ONE, null, BigDecimal.ONE)); } /** - * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#subtract(java.math.BigDecimal, java.math.BigDecimal)}. - */ - @Test - public void testSubtractBigDecimalBigDecimal() { - fail("Not yet implemented"); - } - - /** * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#divide(java.math.BigDecimal, java.math.BigDecimal, int)}. */ @Test @@ -145,14 +173,6 @@ } /** - * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#divide(java.math.BigDecimal, java.math.BigDecimal, int, int)}. - */ - @Test - public void testDivideBigDecimalBigDecimalIntInt() { - fail("Not yet implemented"); - } - - /** * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#multiply(java.math.BigDecimal, java.math.BigDecimal)}. */ @Test @@ -257,14 +277,6 @@ } /** - * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#setScale(java.math.BigDecimal, int)}. - */ - @Test - public void testSetScaleBigDecimalInt() { - fail("Not yet implemented"); - } - - /** * Test method for {@link net.objectlab.kit.util.BigDecimalUtil#setScale(java.math.BigDecimal, java.lang.Integer)}. */ @Test Modified: trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java 2010-03-26 21:32:38 UTC (rev 317) +++ trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java 2010-03-28 10:55:09 UTC (rev 318) @@ -1,14 +1,18 @@ package net.objectlab.kit.util; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.HashSet; + import org.junit.Test; public class CollectionUtilTest { @Test public void testIsEmpty() { - fail("Not yet implemented"); + assertTrue("null", CollectionUtil.isEmpty(null)); + assertTrue("empty set", CollectionUtil.isEmpty(new HashSet())); } @Test This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2010-03-28 11:31:41
|
Revision: 319 http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=319&view=rev Author: benoitx Date: 2010-03-28 11:31:34 +0000 (Sun, 28 Mar 2010) Log Message: ----------- 100% coverage for CollectionUtil and make the deliverable an osgi bundle! Modified Paths: -------------- trunk/utils/pom.xml trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java Modified: trunk/utils/pom.xml =================================================================== --- trunk/utils/pom.xml 2010-03-28 10:55:09 UTC (rev 318) +++ trunk/utils/pom.xml 2010-03-28 11:31:34 UTC (rev 319) @@ -6,7 +6,7 @@ <groupId>net.objectlab.kit</groupId> <artifactId>objectlab-utils</artifactId> <version>1.0-SNAPSHOT</version> - <packaging>jar</packaging> + <packaging>bundle</packaging> <name>ObjectLab Kit - General Utilities</name> @@ -39,6 +39,20 @@ <target>1.5</target> </configuration> </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package>net.objectlab.kit.*;version="${pom.version}"</Export-Package> + <Private-Package /> + <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> + <Bundle-Version>${pom.version}</Bundle-Version> + <Bundle-RequiredExecutionEnvironment>J2SE-1.5</Bundle-RequiredExecutionEnvironment> + </instructions> + </configuration> + </plugin> </plugins> </build> Modified: trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java 2010-03-28 10:55:09 UTC (rev 318) +++ trunk/utils/src/main/java/net/objectlab/kit/util/CollectionUtil.java 2010-03-28 11:31:34 UTC (rev 319) @@ -103,6 +103,9 @@ * @return true if none of the collections are empty or null */ public static boolean noneEmpty(final Collection... collections) { + if (collections == null) { + return false; + } for (final Collection col : collections) { if (isEmpty(col)) { return false; @@ -112,8 +115,11 @@ } public static boolean sameContent(final Collection c1, final Collection c2) { - boolean same = c1 != null && c2 != null || c1 == c2 || c1 == null && c2 == null; - if (same && c1 != null) { + if (c1 == c2 || (isEmpty(c2) && isEmpty(c1))) { + return true; + } + boolean same = false; + if (c1 != null && c2 != null) { same = c1.size() == c2.size(); if (same) { same = c1.equals(c2); Modified: trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java 2010-03-28 10:55:09 UTC (rev 318) +++ trunk/utils/src/test/java/net/objectlab/kit/util/CollectionUtilTest.java 2010-03-28 11:31:34 UTC (rev 319) @@ -1,8 +1,12 @@ package net.objectlab.kit.util; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import org.junit.Test; @@ -12,47 +16,116 @@ @Test public void testIsEmpty() { assertTrue("null", CollectionUtil.isEmpty(null)); - assertTrue("empty set", CollectionUtil.isEmpty(new HashSet())); + HashSet col = new HashSet(); + assertTrue("empty set", CollectionUtil.isEmpty(col)); + col.add(1); + assertFalse("not empty set", CollectionUtil.isEmpty(col)); + col.add(2); + assertFalse("not empty set", CollectionUtil.isEmpty(col)); } @Test - public void testIsNotEmptyCollectionOfQ() { - fail("Not yet implemented"); + public void testIsNotEmptyCollection() { + assertFalse("null", CollectionUtil.isNotEmpty((Collection) null)); + HashSet col = new HashSet(); + assertFalse("empty set", CollectionUtil.isNotEmpty(col)); + col.add(1); + assertTrue("not empty set", CollectionUtil.isNotEmpty(col)); + col.add(2); + assertTrue("2 empty set", CollectionUtil.isNotEmpty(col)); } @Test - public void testHasOneItem() { - fail("Not yet implemented"); + public void testIsNotEmptyObjectArray() { + assertFalse("null", CollectionUtil.isNotEmpty((Object[]) null)); + Object[] col = new String[0]; + assertFalse("empty set", CollectionUtil.isNotEmpty(col)); + col = new String[1]; + assertTrue("not empty set", CollectionUtil.isNotEmpty(col)); } @Test - public void testIsNotEmptyObjectArray() { - fail("Not yet implemented"); + public void testHasOneItem() { + assertFalse("null", CollectionUtil.hasOneItem((Collection) null)); + HashSet col = new HashSet(); + assertFalse("empty set", CollectionUtil.hasOneItem(col)); + col.add(1); + assertTrue("not empty set", CollectionUtil.hasOneItem(col)); + col.add(2); + assertFalse("2 items set", CollectionUtil.hasOneItem(col)); } @Test public void testSize() { - fail("Not yet implemented"); + assertEquals("null", 0, CollectionUtil.size((Collection) null)); + HashSet col = new HashSet(); + assertEquals("empty ", 0, CollectionUtil.size(col)); + col.add(1); + assertEquals("1", 1, CollectionUtil.size(col)); + col.add(2); + assertEquals("2", 2, CollectionUtil.size(col)); } @Test public void testContains() { - fail("Not yet implemented"); + assertFalse("null", CollectionUtil.contains((Collection) null, "bla")); + HashSet col = new HashSet(); + assertFalse("empty", CollectionUtil.contains(col, "bla")); + col.add(1); + assertFalse("1", CollectionUtil.contains(col, "bla")); + col.add("bla"); + assertTrue("1 + bla", CollectionUtil.contains(col, "bla")); } @Test public void testContainsAny() { - fail("Not yet implemented"); + assertFalse("null", CollectionUtil.containsAny((Collection) null, "bla", 2)); + HashSet col = new HashSet(); + assertFalse("empty", CollectionUtil.containsAny(col, "bla", 2)); + col.add(1); + assertFalse("contains 1", CollectionUtil.containsAny(col, "bla", 2)); + col.add(2); + assertTrue("contains 2", CollectionUtil.containsAny(col, "bla", 2)); + col.add("bla"); + assertTrue("contains 2 and bla", CollectionUtil.containsAny(col, "bla", 2)); + col.remove(2); + assertTrue("contains bla", CollectionUtil.containsAny(col, "bla", 2)); + col.remove("bla"); + assertFalse("contains nothing", CollectionUtil.containsAny(col, "bla", 2)); } @Test public void testNoneEmpty() { - fail("Not yet implemented"); + assertFalse("1 null", CollectionUtil.noneEmpty(null)); + assertFalse("2 null", CollectionUtil.noneEmpty(null, null)); + assertFalse("empty null", CollectionUtil.noneEmpty(Collections.EMPTY_LIST, null)); + assertFalse("empty empty", CollectionUtil.noneEmpty(Collections.EMPTY_LIST, Collections.EMPTY_LIST)); + HashSet col = new HashSet(); + assertFalse("empty empty", CollectionUtil.noneEmpty(col, col)); + col.add(1); + assertTrue("1", CollectionUtil.noneEmpty(col)); + HashSet col2 = new HashSet(); + assertFalse("1 and 2 empty", CollectionUtil.noneEmpty(col, col2)); + assertFalse("1 and null 2 empty", CollectionUtil.noneEmpty(col, null, col2)); + col2.add("bla"); + assertFalse("1 and null 2", CollectionUtil.noneEmpty(col, null, col2)); + assertTrue("1 and 2", CollectionUtil.noneEmpty(col, col2)); } @Test public void testSameContent() { - fail("Not yet implemented"); + assertTrue("null null", CollectionUtil.sameContent(null, null)); + HashSet col = new HashSet(); + assertTrue("empty null", CollectionUtil.sameContent(col, null)); + assertTrue("same", CollectionUtil.sameContent(col, col)); + HashSet col1 = new HashSet(); + col.add(1); + assertFalse("not same empty 1", CollectionUtil.sameContent(col, col1)); + assertFalse("not same 1 empty", CollectionUtil.sameContent(col1, col)); + col1.add(1); + assertTrue("same 1 1", CollectionUtil.sameContent(col1, col)); + col1.add(2); + assertFalse("same 1 2", CollectionUtil.sameContent(col1, col)); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2010-05-19 20:18:29
|
Revision: 371 http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=371&view=rev Author: benoitx Date: 2010-05-19 20:18:22 +0000 (Wed, 19 May 2010) Log Message: ----------- Ready for a quick release for CS. Modified Paths: -------------- trunk/utils/pom.xml trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java Modified: trunk/utils/pom.xml =================================================================== --- trunk/utils/pom.xml 2010-05-10 14:12:20 UTC (rev 370) +++ trunk/utils/pom.xml 2010-05-19 20:18:22 UTC (rev 371) @@ -5,7 +5,7 @@ <groupId>net.objectlab.kit</groupId> <artifactId>objectlab-utils</artifactId> - <version>1.0-SNAPSHOT</version> + <version>1.0-cs</version> <packaging>bundle</packaging> <name>ObjectLab Kit - General Utilities</name> Modified: trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java 2010-05-10 14:12:20 UTC (rev 370) +++ trunk/utils/src/main/java/net/objectlab/kit/util/BigDecimalUtil.java 2010-05-19 20:18:22 UTC (rev 371) @@ -682,4 +682,12 @@ public static boolean isZeroOrLess(final BigDecimal value) { return value != null && value.signum() <= 0; } + + /** + * Return the decimal part of the value. + * @param val + */ + public static BigDecimal decimalPart(final BigDecimal val) { + return BigDecimalUtil.subtract(val, val.setScale(0, BigDecimal.ROUND_DOWN)); + } } Modified: trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java 2010-05-10 14:12:20 UTC (rev 370) +++ trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java 2010-05-19 20:18:22 UTC (rev 371) @@ -418,8 +418,12 @@ return b.toString(); } - public static Object emptyIfNull(final Object txt) { - return txt != null ? txt : EMPTY; + /** + * Returns "" if obj is null, obj.toString() otherwise. + * @param obj + */ + public static String emptyIfNull(final Object obj) { + return obj != null ? obj.toString() : EMPTY; } public static boolean isWildcardOrNull(final String txt) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <be...@us...> - 2010-11-21 15:45:47
|
Revision: 379 http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=379&view=rev Author: benoitx Date: 2010-11-21 15:45:38 +0000 (Sun, 21 Nov 2010) Log Message: ----------- Thanks to Tom Folga for the following suggestions: 1) Should be called ReadOnly not Immutable... 2) Externalize the current time 3) some multithreading suggestions. 4) added stop() Modified Paths: -------------- trunk/utils/pom.xml Added Paths: ----------- trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollection.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollectionBuilder.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapBuilder.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetBuilder.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringMap.java trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringSet.java trunk/utils/src/main/java/net/objectlab/kit/collections/SystemTimeProvider.java trunk/utils/src/main/java/net/objectlab/kit/collections/TimeProvider.java trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetTest.java Removed Paths: ------------- trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractImmutabeExpiringCollection.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollection.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollectionBuilder.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMap.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMapBuilder.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSet.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSetBuilder.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringMap.java trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringSet.java trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashMapTest.java trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashSetTest.java Modified: trunk/utils/pom.xml =================================================================== --- trunk/utils/pom.xml 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/pom.xml 2010-11-21 15:45:38 UTC (rev 379) @@ -5,7 +5,7 @@ <groupId>net.objectlab.kit</groupId> <artifactId>objectlab-utils</artifactId> - <version>1.0-cs</version> + <version>1.0-SNAPSHOT</version> <packaging>bundle</packaging> <name>ObjectLab Kit - General Utilities</name> Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractImmutabeExpiringCollection.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractImmutabeExpiringCollection.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractImmutabeExpiringCollection.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,104 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import java.util.Timer; -import java.util.TimerTask; - -/** - * @author xhensevalb - */ -public abstract class AbstractImmutabeExpiringCollection { - private long expiryTimeoutMilliseconds; - private boolean reloadOnExpiry = true; - private boolean reloadWhenExpired = true; - private boolean loadOnFirstAccess = true; - private long lastLoadingTime; - private String id; - private Timer timer; - - protected void setId(final String id) { - this.id = id; - } - - public String getId() { - return id; - } - - protected void setLastLoadingTime(final long lastLoadingTime) { - this.lastLoadingTime = lastLoadingTime; - } - - public void setExpiryTimeoutMilliseconds(final long milliseconds) { - this.expiryTimeoutMilliseconds = milliseconds; - } - - public void setReloadOnExpiry(final boolean reloadOnExpiry) { - this.reloadOnExpiry = reloadOnExpiry; - } - - public void setLoadOnFirstAccess(final boolean loadOnFirstAccess) { - this.loadOnFirstAccess = loadOnFirstAccess; - } - - public void setReloadWhenExpired(final boolean reloadWhenExpired) { - this.reloadWhenExpired = reloadWhenExpired; - } - - protected boolean hasExpired() { - return System.currentTimeMillis() - lastLoadingTime > expiryTimeoutMilliseconds; - } - - public void start() { - if (reloadOnExpiry) { - // start timer - timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - load(); - } - }, 0, expiryTimeoutMilliseconds); - } - if (!loadOnFirstAccess) { - load(); - } - } - - protected void validateOnAccess() { - if (hasExpired()) { - if (reloadWhenExpired || loadOnFirstAccess && lastLoadingTime == 0) { - load(); - } else { - doClear(); - } - } - } - - private void load() { - doLoad(); - lastLoadingTime = System.currentTimeMillis(); - } - - protected abstract void doLoad(); - - protected abstract void doClear(); - - protected long getExpiryTimeoutMilliseconds() { - return expiryTimeoutMilliseconds; - } - - protected boolean isReloadOnExpiry() { - return reloadOnExpiry; - } - - protected boolean isLoadOnFirstAccess() { - return loadOnFirstAccess; - } - - protected long getLastLoadingTime() { - return lastLoadingTime; - } - -} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java (from rev 376, trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractImmutabeExpiringCollection.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,121 @@ +/** + * + */ +package net.objectlab.kit.collections; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * @author xhensevalb + */ +public abstract class AbstractReadOnlyExpiringCollection { + private TimeProvider timeProvider; + private long expiryTimeoutMilliseconds; + private boolean reloadOnExpiry = true; + private boolean reloadWhenExpired = true; + private boolean loadOnFirstAccess = true; + private long lastLoadingTime; + private String id; + private Timer timer; + + protected void setId(final String id) { + this.id = id; + } + + protected void setTimeProvider(final TimeProvider timeProvider) { + this.timeProvider = timeProvider != null ? timeProvider : new SystemTimeProvider(); + } + + public String getId() { + return id; + } + + public void setExpiryTimeoutMilliseconds(final long milliseconds) { + this.expiryTimeoutMilliseconds = milliseconds; + } + + public void setReloadOnExpiry(final boolean reloadOnExpiry) { + this.reloadOnExpiry = reloadOnExpiry; + } + + public void setLoadOnFirstAccess(final boolean loadOnFirstAccess) { + this.loadOnFirstAccess = loadOnFirstAccess; + } + + public void setReloadWhenExpired(final boolean reloadWhenExpired) { + this.reloadWhenExpired = reloadWhenExpired; + } + + protected boolean hasExpired() { + return timeProvider.getCurrentTimeMillis() - lastLoadingTime > expiryTimeoutMilliseconds; + } + + public void start() { + if (reloadOnExpiry) { + // start timer + timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + load(); + } + }, 0, expiryTimeoutMilliseconds); + } + if (!loadOnFirstAccess) { + load(); + } + } + + public void stop() { + if (timer != null) { + timer.cancel(); + timer.purge(); + timer = null; + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + stop(); + } + + protected void validateOnAccess() { + if (hasExpired()) { + if (reloadWhenExpired || loadOnFirstAccess && lastLoadingTime == 0) { + load(); + } else { + doClear(); + } + } + } + + private synchronized void load() { + if (hasExpired()) { + doLoad(); + lastLoadingTime = timeProvider.getCurrentTimeMillis(); + } + } + + protected abstract void doLoad(); + + protected abstract void doClear(); + + protected long getExpiryTimeoutMilliseconds() { + return expiryTimeoutMilliseconds; + } + + protected boolean isReloadOnExpiry() { + return reloadOnExpiry; + } + + protected boolean isLoadOnFirstAccess() { + return loadOnFirstAccess; + } + + protected long getLastLoadingTime() { + return lastLoadingTime; + } + +} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollection.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollection.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollection.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,22 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -/** - * @author xhensevalb - * - */ -public interface ImmutableExpiringCollection { - void setExpiryTimeoutMilliseconds(long milliseconds); - - void setLoadOnFirstAccess(boolean loadOnFirstAccess); - - void setReloadOnExpiry(boolean loadOnExpiry); - - void setReloadWhenExpired(boolean loadWhenExpired); - - void start(); - - void reload(); -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollectionBuilder.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollectionBuilder.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollectionBuilder.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,74 +0,0 @@ -package net.objectlab.kit.collections; - -import net.objectlab.kit.util.PeriodBuilder; - -/** - * Builder for the immutable classes. - * Default: - * - no time out - * - reload on expiry: true - * - load on first access only (ie not at construction time) - * - * @author xhensevalb - * - */ -public class ImmutableExpiringCollectionBuilder { - private long expiryTimeoutMilliseconds = -1; - private boolean reloadOnExpiry = true; - private boolean reloadWhenExpired = true; - private boolean loadOnFirstAccess = true; - private String id; - - public ImmutableExpiringCollectionBuilder id(final String id) { - this.id = id; - return this; - } - - public ImmutableExpiringCollectionBuilder expiryTimeout(final PeriodBuilder expiryTimeout) { - assert expiryTimeout != null; - if (expiryTimeout != null) { - this.expiryTimeoutMilliseconds = expiryTimeout.calculateMilliseconds(); - } - return this; - } - - public ImmutableExpiringCollectionBuilder expiryTimeoutMilliseconds(final long expiryTimeoutMilliseconds) { - this.expiryTimeoutMilliseconds = expiryTimeoutMilliseconds; - return this; - } - - public ImmutableExpiringCollectionBuilder reloadWhenExpired(final boolean reloadWhenExpired) { - this.reloadWhenExpired = reloadWhenExpired; - return this; - } - - public ImmutableExpiringCollectionBuilder reloadOnExpiry(final boolean loadOnExpiry) { - this.reloadOnExpiry = loadOnExpiry; - return this; - } - - public ImmutableExpiringCollectionBuilder loadOnFirstAccess(final boolean loadOnFirstAccess) { - this.loadOnFirstAccess = loadOnFirstAccess; - return this; - } - - final long getExpiryTimeoutMilliseconds() { - return expiryTimeoutMilliseconds; - } - - final boolean isReloadOnExpiry() { - return reloadOnExpiry; - } - - final boolean isReloadWhenExpired() { - return reloadWhenExpired; - } - - final boolean isLoadOnFirstAccess() { - return loadOnFirstAccess; - } - - final String getId() { - return id; - } -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMap.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMap.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMap.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,100 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -/** - * @author xhensevalb - * - */ -public class ImmutableExpiringHashMap<K, V> extends AbstractImmutabeExpiringCollection implements ImmutableExpiringMap<K, V> { - private Map<K, V> delegate = new HashMap<K, V>(); - private final MapLoader<K, V> loader; - - public ImmutableExpiringHashMap(final ImmutableExpiringHashMapBuilder<K, V> builder) { - loader = builder.getLoader(); - setId(builder.getId()); - setExpiryTimeoutMilliseconds(builder.getExpiryTimeoutMilliseconds()); - setReloadOnExpiry(builder.isReloadOnExpiry()); - setLoadOnFirstAccess(builder.isLoadOnFirstAccess()); - setReloadWhenExpired(builder.isReloadWhenExpired()); - start(); - } - - @Override - protected void doLoad() { - final DefaultMapBuilder<K, V> builder = new DefaultMapBuilder<K, V>(getId()); - loader.load(builder); - delegate = builder.build(); - } - - public void clear() { - throw new IllegalAccessError("Collection is immutable"); - } - - public boolean containsKey(final Object key) { - validateOnAccess(); - return delegate.containsKey(key); - } - - public boolean containsValue(final Object value) { - validateOnAccess(); - return delegate.containsValue(value); - } - - public Set<java.util.Map.Entry<K, V>> entrySet() { - validateOnAccess(); - return delegate.entrySet(); - } - - public V get(final Object key) { - validateOnAccess(); - return delegate.get(key); - } - - public boolean isEmpty() { - validateOnAccess(); - return delegate.isEmpty(); - } - - public Set<K> keySet() { - validateOnAccess(); - return delegate.keySet(); - } - - public V put(final K key, final V value) { - throw new IllegalAccessError("Collection is immutable"); - } - - public void putAll(final Map<? extends K, ? extends V> m) { - throw new IllegalAccessError("Collection is immutable"); - } - - public V remove(final Object key) { - throw new IllegalAccessError("Collection is immutable"); - } - - public int size() { - validateOnAccess(); - return delegate.size(); - } - - public Collection<V> values() { - validateOnAccess(); - return delegate.values(); - } - - @Override - protected void doClear() { - delegate.clear(); - } - - public void reload() { - doLoad(); - } -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMapBuilder.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMapBuilder.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMapBuilder.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,14 +0,0 @@ -package net.objectlab.kit.collections; - -public class ImmutableExpiringHashMapBuilder<K, V> extends ImmutableExpiringCollectionBuilder { - private final MapLoader<K, V> loader; - - public ImmutableExpiringHashMapBuilder(final MapLoader<K, V> loader) { - super(); - this.loader = loader; - } - - final MapLoader<K, V> getLoader() { - return loader; - } -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSet.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSet.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSet.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,104 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -/** - * @author xhensevalb - * - */ -public class ImmutableExpiringHashSet<T> extends AbstractImmutabeExpiringCollection implements ImmutableExpiringSet<T> { - private final SetLoader<T> loader; - private Set<T> delegate = new HashSet<T>(); - - public ImmutableExpiringHashSet(final ImmutableExpiringHashSetBuilder<T> builder) { - this.loader = builder.getLoader(); - setId(builder.getId()); - setExpiryTimeoutMilliseconds(builder.getExpiryTimeoutMilliseconds()); - setReloadOnExpiry(builder.isReloadOnExpiry()); - setLoadOnFirstAccess(builder.isLoadOnFirstAccess()); - setReloadWhenExpired(builder.isReloadWhenExpired()); - start(); - } - - @Override - protected void doLoad() { - final DefaultSetBuilder<T> builder = new DefaultSetBuilder<T>(getId()); - loader.load(builder); - delegate = builder.build(); - } - - public boolean add(final T e) { - validateOnAccess(); - return delegate.add(e); - } - - public boolean addAll(final Collection<? extends T> c) { - throw new IllegalAccessError("Collection is immutable"); - } - - public void clear() { - throw new IllegalAccessError("Collection is immutable"); - } - - public boolean contains(final Object o) { - validateOnAccess(); - return delegate.contains(o); - } - - public boolean containsAll(final Collection<?> c) { - validateOnAccess(); - return delegate.containsAll(c); - } - - public boolean isEmpty() { - validateOnAccess(); - return delegate.isEmpty(); - } - - public Iterator<T> iterator() { - validateOnAccess(); - return delegate.iterator(); - } - - public boolean remove(final Object o) { - throw new IllegalAccessError("Collection is immutable"); - } - - public boolean removeAll(final Collection<?> c) { - throw new IllegalAccessError("Collection is immutable"); - } - - public boolean retainAll(final Collection<?> c) { - throw new IllegalAccessError("Collection is immutable"); - } - - public int size() { - validateOnAccess(); - return delegate.size(); - } - - public Object[] toArray() { - validateOnAccess(); - return delegate.toArray(); - } - - public <T> T[] toArray(final T[] a) { - validateOnAccess(); - return delegate.toArray(a); - } - - @Override - protected void doClear() { - delegate.clear(); - } - - public void reload() { - doLoad(); - } -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSetBuilder.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSetBuilder.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSetBuilder.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,21 +0,0 @@ -package net.objectlab.kit.collections; - -/** - * The builder in charge of providing the parameters for the Set. - * - * @author xhensevalb - * - * @param <T> - */ -public class ImmutableExpiringHashSetBuilder<T> extends ImmutableExpiringCollectionBuilder { - private final SetLoader<T> loader; - - public ImmutableExpiringHashSetBuilder(final SetLoader<T> loader) { - super(); - this.loader = loader; - } - - final SetLoader<T> getLoader() { - return loader; - } -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringMap.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringMap.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringMap.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,13 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import java.util.Map; - -/** - * @author xhensevalb - * - */ -public interface ImmutableExpiringMap<K, V> extends ImmutableExpiringCollection, Map<K, V> { -} Deleted: trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringSet.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringSet.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringSet.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,13 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import java.util.Set; - -/** - * @author xhensevalb - * - */ -public interface ImmutableExpiringSet<T> extends ImmutableExpiringCollection, Set<T> { -} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollection.java (from rev 377, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollection.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollection.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollection.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,23 @@ +/** + * + */ +package net.objectlab.kit.collections; + +/** + * @author xhensevalb + * + */ +public interface ReadOnlyExpiringCollection { + // void setExpiryTimeoutMilliseconds(long milliseconds); + + // void setLoadOnFirstAccess(boolean loadOnFirstAccess); + + // void setReloadOnExpiry(boolean loadOnExpiry); + + // void setReloadWhenExpired(boolean loadWhenExpired); + + // void start(); + void stop(); + + void reload(); +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollectionBuilder.java (from rev 376, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringCollectionBuilder.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollectionBuilder.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringCollectionBuilder.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,84 @@ +package net.objectlab.kit.collections; + +import net.objectlab.kit.util.PeriodBuilder; + +/** + * Builder for the immutable classes. + * Default: + * - no time out + * - reload on expiry: true + * - load on first access only (ie not at construction time) + * + * @author xhensevalb + * + */ +public class ReadOnlyExpiringCollectionBuilder { + private long expiryTimeoutMilliseconds = -1; + private boolean reloadOnExpiry = true; + private boolean reloadWhenExpired = true; + private boolean loadOnFirstAccess = true; + private String id; + private TimeProvider timeProvider; + + public ReadOnlyExpiringCollectionBuilder timeProvider(final TimeProvider timeProvider) { + this.timeProvider = timeProvider; + return this; + } + + public ReadOnlyExpiringCollectionBuilder id(final String id) { + this.id = id; + return this; + } + + public ReadOnlyExpiringCollectionBuilder expiryTimeout(final PeriodBuilder expiryTimeout) { + assert expiryTimeout != null; + if (expiryTimeout != null) { + this.expiryTimeoutMilliseconds = expiryTimeout.calculateMilliseconds(); + } + return this; + } + + public ReadOnlyExpiringCollectionBuilder expiryTimeoutMilliseconds(final long expiryTimeoutMilliseconds) { + this.expiryTimeoutMilliseconds = expiryTimeoutMilliseconds; + return this; + } + + public ReadOnlyExpiringCollectionBuilder reloadWhenExpired(final boolean reloadWhenExpired) { + this.reloadWhenExpired = reloadWhenExpired; + return this; + } + + public ReadOnlyExpiringCollectionBuilder reloadOnExpiry(final boolean loadOnExpiry) { + this.reloadOnExpiry = loadOnExpiry; + return this; + } + + public ReadOnlyExpiringCollectionBuilder loadOnFirstAccess(final boolean loadOnFirstAccess) { + this.loadOnFirstAccess = loadOnFirstAccess; + return this; + } + + final long getExpiryTimeoutMilliseconds() { + return expiryTimeoutMilliseconds; + } + + final boolean isReloadOnExpiry() { + return reloadOnExpiry; + } + + final boolean isReloadWhenExpired() { + return reloadWhenExpired; + } + + final boolean isLoadOnFirstAccess() { + return loadOnFirstAccess; + } + + final String getId() { + return id; + } + + final TimeProvider getTimeProvider() { + return timeProvider; + } +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java (from rev 377, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMap.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,100 @@ +/** + * + */ +package net.objectlab.kit.collections; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author xhensevalb + * + */ +public class ReadOnlyExpiringHashMap<K, V> extends AbstractReadOnlyExpiringCollection implements ReadOnlyExpiringMap<K, V> { + private Map<K, V> delegate = new HashMap<K, V>(); + private final MapLoader<K, V> loader; + + public ReadOnlyExpiringHashMap(final ReadOnlyExpiringHashMapBuilder<K, V> builder) { + loader = builder.getLoader(); + setId(builder.getId()); + setExpiryTimeoutMilliseconds(builder.getExpiryTimeoutMilliseconds()); + setReloadOnExpiry(builder.isReloadOnExpiry()); + setLoadOnFirstAccess(builder.isLoadOnFirstAccess()); + setReloadWhenExpired(builder.isReloadWhenExpired()); + start(); + } + + @Override + protected void doLoad() { + final DefaultMapBuilder<K, V> builder = new DefaultMapBuilder<K, V>(getId()); + loader.load(builder); + delegate = builder.build(); + } + + public void clear() { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public boolean containsKey(final Object key) { + validateOnAccess(); + return delegate.containsKey(key); + } + + public boolean containsValue(final Object value) { + validateOnAccess(); + return delegate.containsValue(value); + } + + public Set<java.util.Map.Entry<K, V>> entrySet() { + validateOnAccess(); + return delegate.entrySet(); + } + + public V get(final Object key) { + validateOnAccess(); + return delegate.get(key); + } + + public boolean isEmpty() { + validateOnAccess(); + return delegate.isEmpty(); + } + + public Set<K> keySet() { + validateOnAccess(); + return delegate.keySet(); + } + + public V put(final K key, final V value) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public void putAll(final Map<? extends K, ? extends V> m) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public V remove(final Object key) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public int size() { + validateOnAccess(); + return delegate.size(); + } + + public Collection<V> values() { + validateOnAccess(); + return delegate.values(); + } + + @Override + protected void doClear() { + delegate.clear(); + } + + public void reload() { + doLoad(); + } +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapBuilder.java (from rev 375, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashMapBuilder.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapBuilder.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapBuilder.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,14 @@ +package net.objectlab.kit.collections; + +public class ReadOnlyExpiringHashMapBuilder<K, V> extends ReadOnlyExpiringCollectionBuilder { + private final MapLoader<K, V> loader; + + public ReadOnlyExpiringHashMapBuilder(final MapLoader<K, V> loader) { + super(); + this.loader = loader; + } + + final MapLoader<K, V> getLoader() { + return loader; + } +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java (from rev 377, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSet.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,104 @@ +/** + * + */ +package net.objectlab.kit.collections; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * @author xhensevalb + * + */ +public class ReadOnlyExpiringHashSet<T> extends AbstractReadOnlyExpiringCollection implements ReadOnlyExpiringSet<T> { + private final SetLoader<T> loader; + private Set<T> delegate = new HashSet<T>(); + + public ReadOnlyExpiringHashSet(final ReadOnlyExpiringHashSetBuilder<T> builder) { + this.loader = builder.getLoader(); + setId(builder.getId()); + setExpiryTimeoutMilliseconds(builder.getExpiryTimeoutMilliseconds()); + setReloadOnExpiry(builder.isReloadOnExpiry()); + setLoadOnFirstAccess(builder.isLoadOnFirstAccess()); + setReloadWhenExpired(builder.isReloadWhenExpired()); + start(); + } + + @Override + protected void doLoad() { + final DefaultSetBuilder<T> builder = new DefaultSetBuilder<T>(getId()); + loader.load(builder); + delegate = builder.build(); + } + + public boolean add(final T e) { + validateOnAccess(); + return delegate.add(e); + } + + public boolean addAll(final Collection<? extends T> c) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public void clear() { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public boolean contains(final Object o) { + validateOnAccess(); + return delegate.contains(o); + } + + public boolean containsAll(final Collection<?> c) { + validateOnAccess(); + return delegate.containsAll(c); + } + + public boolean isEmpty() { + validateOnAccess(); + return delegate.isEmpty(); + } + + public Iterator<T> iterator() { + validateOnAccess(); + return delegate.iterator(); + } + + public boolean remove(final Object o) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public boolean removeAll(final Collection<?> c) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public boolean retainAll(final Collection<?> c) { + throw new UnsupportedOperationException("Collection is immutable"); + } + + public int size() { + validateOnAccess(); + return delegate.size(); + } + + public Object[] toArray() { + validateOnAccess(); + return delegate.toArray(); + } + + public <T> T[] toArray(final T[] a) { + validateOnAccess(); + return delegate.toArray(a); + } + + @Override + protected void doClear() { + delegate.clear(); + } + + public void reload() { + doLoad(); + } +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetBuilder.java (from rev 376, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringHashSetBuilder.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetBuilder.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetBuilder.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,21 @@ +package net.objectlab.kit.collections; + +/** + * The builder in charge of providing the parameters for the Set. + * + * @author xhensevalb + * + * @param <T> + */ +public class ReadOnlyExpiringHashSetBuilder<T> extends ReadOnlyExpiringCollectionBuilder { + private final SetLoader<T> loader; + + public ReadOnlyExpiringHashSetBuilder(final SetLoader<T> loader) { + super(); + this.loader = loader; + } + + final SetLoader<T> getLoader() { + return loader; + } +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringMap.java (from rev 375, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringMap.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringMap.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringMap.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,13 @@ +/** + * + */ +package net.objectlab.kit.collections; + +import java.util.Map; + +/** + * @author xhensevalb + * + */ +public interface ReadOnlyExpiringMap<K, V> extends ReadOnlyExpiringCollection, Map<K, V> { +} Copied: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringSet.java (from rev 375, trunk/utils/src/main/java/net/objectlab/kit/collections/ImmutableExpiringSet.java) =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringSet.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringSet.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,13 @@ +/** + * + */ +package net.objectlab.kit.collections; + +import java.util.Set; + +/** + * @author xhensevalb + * + */ +public interface ReadOnlyExpiringSet<T> extends ReadOnlyExpiringCollection, Set<T> { +} Added: trunk/utils/src/main/java/net/objectlab/kit/collections/SystemTimeProvider.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/SystemTimeProvider.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/SystemTimeProvider.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,19 @@ +/** + * + */ +package net.objectlab.kit.collections; + +/** + * @author xhensevalb + * + */ +public class SystemTimeProvider implements TimeProvider { + + /* (non-Javadoc) + * @see net.objectlab.kit.collections.TimeProvider#getCurrentMillis() + */ + public long getCurrentTimeMillis() { + return System.currentTimeMillis(); + } + +} Added: trunk/utils/src/main/java/net/objectlab/kit/collections/TimeProvider.java =================================================================== --- trunk/utils/src/main/java/net/objectlab/kit/collections/TimeProvider.java (rev 0) +++ trunk/utils/src/main/java/net/objectlab/kit/collections/TimeProvider.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,12 @@ +/** + * + */ +package net.objectlab.kit.collections; + +/** + * @author xhensevalb + * + */ +public interface TimeProvider { + long getCurrentTimeMillis(); +} Deleted: trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashMapTest.java =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashMapTest.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashMapTest.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,228 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.junit.Test; - -/** - * @author xhensevalb - * - */ -public class ImmutableExpiringHashMapTest implements MapLoader<String, Integer> { - - private int reloadCount; - - @Before - public void reset() { - reloadCount = 0; - } - - @Test - public void basicConstructorNoReload() { - final ImmutableExpiringHashMapBuilder<String, Integer> builder = new ImmutableExpiringHashMapBuilder<String, Integer>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(true); - builder.reloadOnExpiry(false); - builder.reloadWhenExpired(false); - builder.id("Greetings"); - - final ImmutableExpiringMap<String, Integer> ims = new ImmutableExpiringHashMap<String, Integer>(builder); - - assertEquals("Should not call load until called", 0, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(101); - } catch (final InterruptedException e) { - } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - // should be gone - assertTrue(ims.isEmpty()); - assertEquals(0, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertFalse("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertNull(ims.get("Yo")); - } - - @Test - public void basicConstructorWithReloadWhenCalled() { - final ImmutableExpiringHashMapBuilder<String, Integer> builder = new ImmutableExpiringHashMapBuilder<String, Integer>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(true); - builder.reloadOnExpiry(false); - builder.reloadWhenExpired(true); - builder.id("Greetings"); - - final ImmutableExpiringMap<String, Integer> ims = new ImmutableExpiringHashMap<String, Integer>(builder); - - assertEquals("Should not call load until called", 0, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(101); - } catch (final InterruptedException e) { - } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - assertEquals("2) Should not call load until called", 1, reloadCount); - - // should be gone - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(2, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - } - - @Test - public void basicConstructorWithImmediateLoadAndWhenExpired() { - final ImmutableExpiringHashMapBuilder<String, Integer> builder = new ImmutableExpiringHashMapBuilder<String, Integer>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(false); - builder.reloadOnExpiry(false); - builder.reloadWhenExpired(true); - builder.id("Greetings"); - - final ImmutableExpiringMap<String, Integer> ims = new ImmutableExpiringHashMap<String, Integer>(builder); - - assertEquals("Should not call load until called", 1, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(101); - } catch (final InterruptedException e) { - } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - assertEquals("2) Should not call load until called", 1, reloadCount); - - // should be gone - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(2, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - } - - @Test - public void basicConstructorWithReloadOnExpiry() { - final ImmutableExpiringHashMapBuilder<String, Integer> builder = new ImmutableExpiringHashMapBuilder<String, Integer>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(false); - builder.reloadOnExpiry(true); - builder.reloadWhenExpired(false); - builder.id("Greetings"); - - final ImmutableExpiringMap<String, Integer> ims = new ImmutableExpiringHashMap<String, Integer>(builder); - - assertEquals("Should not call load until called", 1, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - - // second call - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - assertEquals("2) Should not call load until called", 2, reloadCount); - - // should be gone - assertFalse(ims.isEmpty()); - assertEquals(2, ims.size()); - assertEquals(2, reloadCount); - assertFalse("diff key", ims.containsKey("Hi")); - assertTrue("Correct key", ims.containsKey("Hello")); - assertNull("diff key", ims.get("Hi")); - assertEquals(Integer.valueOf(2), ims.get("Yo")); - } - - public void load(final MapBuilder<String, Integer> builder) { - assertEquals("Greetings", builder.getId()); - builder.put("Hello", 1); - builder.put("Yo", 2); - reloadCount++; - } - -} Deleted: trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashSetTest.java =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashSetTest.java 2010-11-21 12:02:45 UTC (rev 378) +++ trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashSetTest.java 2010-11-21 15:45:38 UTC (rev 379) @@ -1,205 +0,0 @@ -/** - * - */ -package net.objectlab.kit.collections; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.junit.Test; - -/** - * @author xhensevalb - * - */ -public class ImmutableExpiringHashSetTest implements SetLoader<String> { - - private int reloadCount; - - @Before - public void reset() { - reloadCount = 0; - } - - @Test - public void basicConstructorNoReload() { - final ImmutableExpiringHashSetBuilder<String> builder = new ImmutableExpiringHashSetBuilder<String>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(true); - builder.reloadOnExpiry(false); - builder.reloadWhenExpired(false); - builder.id("Greetings"); - - final ImmutableExpiringSet<String> ims = new ImmutableExpiringHashSet<String>(builder); - - assertEquals("Should not call load until called", 0, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(101); - } catch (final InterruptedException e) { - } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - // should be gone - assertTrue(ims.isEmpty()); - assertEquals(0, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertFalse("Correct key", ims.contains("Hello")); - } - - @Test - public void basicConstructorWithReloadWhenCalled() { - final ImmutableExpiringHashSetBuilder<String> builder = new ImmutableExpiringHashSetBuilder<String>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(true); - builder.reloadOnExpiry(false); - builder.reloadWhenExpired(true); - builder.id("Greetings"); - - final ImmutableExpiringSet<String> ims = new ImmutableExpiringHashSet<String>(builder); - - assertEquals("Should not call load until called", 0, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(101); - } catch (final InterruptedException e) { - } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - assertEquals("Should NOT have reloaded until called!", 1, reloadCount); - - // should be gone - assertFalse(ims.isEmpty()); - assertEquals("Now it should have called the reload", 2, reloadCount); - assertEquals(1, ims.size()); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - } - - @Test - public void basicConstructorWithImmediateLoadAndWhenExpired() { - final ImmutableExpiringHashSetBuilder<String> builder = new ImmutableExpiringHashSetBuilder<String>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(false); - builder.reloadOnExpiry(false); - builder.reloadWhenExpired(true); - builder.id("Greetings"); - - final ImmutableExpiringSet<String> ims = new ImmutableExpiringHashSet<String>(builder); - - assertEquals("Should call load immediately", 1, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(101); - } catch (final InterruptedException e) { - } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - assertEquals("Should NOT have reloaded until called!", 1, reloadCount); - - // should be gone - assertFalse(ims.isEmpty()); - assertEquals("Now it should have called the reload", 2, reloadCount); - assertEquals(1, ims.size()); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - } - - @Test - public void basicConstructorWithReloadOnExpiry() { - final ImmutableExpiringHashSetBuilder<String> builder = new ImmutableExpiringHashSetBuilder<String>(this); - builder.expiryTimeoutMilliseconds(1000); - builder.loadOnFirstAccess(false); - builder.reloadOnExpiry(true); - builder.reloadWhenExpired(false); // but does not matter - builder.id("Greetings"); - - final ImmutableExpiringSet<String> ims = new ImmutableExpiringHashSet<String>(builder); - - assertEquals("Should have called load immediately", 1, reloadCount); - - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - // try { - // Thread.sleep(101); - // } catch (final InterruptedException e) { - // } - - // second call - assertFalse(ims.isEmpty()); - assertEquals(1, ims.size()); - assertEquals(1, reloadCount); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - try { - Thread.sleep(901); - } catch (final InterruptedException e) { - } - - assertEquals("Should have reloaded until called!", 2, reloadCount); - - // should be gone - assertFalse(ims.isEmpty()); - assertEquals("Now it should have called the reload", 2, reloadCount); - assertEquals(1, ims.size()); - assertFalse("diff key", ims.contains("Hi")); - assertTrue("Correct key", ims.contains("Hello")); - } - - public void load(final SetBuilder<String> builder) { - assertEquals("Greetings", builder.getId()); - builder.add("Hello"); - reloadCount++; - } -} Copied: trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java (from rev 376, trunk/utils/src/test/java/net/objectlab/kit/collections/ImmutableExpiringHashMapTest.java) =================================================================== --- trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java (rev 0) +++ trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java 2010-11-21 15:45:38 UTC (rev 379) @@ -0,0 +1,228 @@ +/** + * + */ +package net.objectlab.kit.collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +/** + * @author xhensevalb + * + */ +public class ReadOnlyExpiringHashMapTest implements MapLoader<String, Integer> { + + private int reloadCount; + + @Before + public void reset() { + reloadCount = 0; + } + + @Test + public void basicConstructorNoReload() { + final ReadOnlyExpiringHashMapBuilder<String, Integer> builder = new ReadOnlyExpiringHashMapBuilder<String, Integer>(this); + builder.expiryTimeoutMilliseconds(1000); + builder.loadOnFirstAccess(true); + builder.reloadOnExpiry(false); + builder.reloadWhenExpired(false); + builder.id("Greetings"); + + final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder); + + assertEquals("Should not call load until called", 0, reloadCount); + + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertTrue("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertEquals(Integer.valueOf(2), ims.get("Yo")); + try { + Thread.sleep(101); + } catch (final InterruptedException e) { + } + + // second call + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertTrue("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertEquals(Integer.valueOf(2), ims.get("Yo")); + try { + Thread.sleep(901); + } catch (final InterruptedException e) { + } + + // should be gone + assertTrue(ims.isEmpty()); + assertEquals(0, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertFalse("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertNull(ims.get("Yo")); + } + + @Test + public void basicConstructorWithReloadWhenCalled() { + final ReadOnlyExpiringHashMapBuilder<String, Integer> builder = new ReadOnlyExpiringHashMapBuilder<String, Integer>(this); + builder.expiryTimeoutMilliseconds(1000); + builder.loadOnFirstAccess(true); + builder.reloadOnExpiry(false); + builder.reloadWhenExpired(true); + builder.id("Greetings"); + + final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder); + + assertEquals("Should not call load until called", 0, reloadCount); + + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertTrue("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertEquals(Integer.valueOf(2), ims.get("Yo")); + try { + Thread.sleep(101); + } catch (final InterruptedException e) { + } + + // second call + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertTrue("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertEquals(Integer.valueOf(2), ims.get("Yo")); + try { + Thread.sleep(901); + } catch (final InterruptedException e) { + } + + assertEquals("2) Should not call load until called", 1, reloadCount); + + // should be gone + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(2, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertTrue("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertEquals(Integer.valueOf(2), ims.get("Yo")); + } + + @Test + public void basicConstructorWithImmediateLoadAndWhenExpired() { + final ReadOnlyExpiringHashMapBuilder<String, Integer> builder = new ReadOnlyExpiringHashMapBuilder<String, Integer>(this); + builder.expiryTimeoutMilliseconds(1000); + builder.loadOnFirstAccess(false); + builder.reloadOnExpiry(false); + builder.reloadWhenExpired(true); + builder.id("Greetings"); + + final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder); + + assertEquals("Should not call load until called", 1, reloadCount); + + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assertTrue("Correct key", ims.containsKey("Hello")); + assertNull("diff key", ims.get("Hi")); + assertEquals(Integer.valueOf(2), ims.get("Yo")); + try { + Thread.sleep(101); + } catch (final InterruptedException e) { + } + + // second call + assertFalse(ims.isEmpty()); + assertEquals(2, ims.size()); + assertEquals(1, reloadCount); + assertFalse("diff key", ims.containsKey("Hi")); + assert... [truncated message content] |
From: <ma...@us...> - 2012-04-25 15:28:31
|
Revision: 383 http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=383&view=rev Author: marchy Date: 2012-04-25 15:28:20 +0000 (Wed, 25 Apr 2012) Log Message: ----------- cleanup and prepare for release Modified Paths: -------------- trunk/utils/pom.xml Removed Paths: ------------- trunk/utils/1.0-cs/ Modified: trunk/utils/pom.xml =================================================================== --- trunk/utils/pom.xml 2011-04-14 21:08:02 UTC (rev 382) +++ trunk/utils/pom.xml 2012-04-25 15:28:20 UTC (rev 383) @@ -55,5 +55,30 @@ </plugin> </plugins> </build> + + <scm> + <connection>scm:svn:https://objectlabkit.svn.sourceforge.net/svnroot/objectlabkit/trunk/utils</connection> + <developerConnection>scm:svn:https://objectlabkit.svn.sourceforge.net/svnroot/objectlabkit/trunk/utils</developerConnection> + <url>http://objectlabkit.svn.sourceforge.net/viewvc/objectlabkit/</url> + </scm> + <issueManagement> + <system>sourceforge</system> + <url>https://sourceforge.net/tracker/?group_id=175139</url> + </issueManagement> + + <distributionManagement> + <repository> + <id>sonatype-nexus-staging</id> + <name>Sonatype Nexus Staging Repository</name> + <url>http://oss.sonatype.org/service/local/staging/deploy/maven2/</url> + </repository> + <snapshotRepository> + <id>sonatype-nexus-snapshots</id> + <name>Sonatype Nexus Snapshots Repository</name> + <url>http://oss.sonatype.org/content/repositories/snapshots/</url> + </snapshotRepository> + </distributionManagement> + + </project> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |