|
From: <be...@us...> - 2010-03-30 22:18:50
|
Revision: 335
http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=335&view=rev
Author: benoitx
Date: 2010-03-30 22:18:44 +0000 (Tue, 30 Mar 2010)
Log Message:
-----------
A few more methods
Modified Paths:
--------------
trunk/utils/src/main/java/net/objectlab/kit/util/Average.java
trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java
trunk/utils/src/main/java/net/objectlab/kit/util/WeightedAverage.java
Added Paths:
-----------
trunk/utils/src/main/java/net/objectlab/kit/util/Total.java
Removed Paths:
-------------
trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java
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-30 11:06:50 UTC (rev 334)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/Average.java 2010-03-30 22:18:44 UTC (rev 335)
@@ -41,19 +41,19 @@
*/
public final class Average implements Serializable {
private static final long serialVersionUID = 4630559777899225672L;
- private Sum sum = new Sum();
+ private Total sum = new Total();
private int count = 0;
public Average() {
}
public Average(final BigDecimal start) {
- sum = new Sum(start);
+ sum = new Total(start);
}
public Average(final int scale) {
final BigDecimal bd = new BigDecimal(0);
- sum = new Sum(bd.setScale(scale));
+ sum = new Total(bd.setScale(scale));
}
public void add(final BigDecimal val) {
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-30 11:06:50 UTC (rev 334)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java 2010-03-30 22:18:44 UTC (rev 335)
@@ -517,4 +517,38 @@
}
return EMPTY;
}
+
+ /**
+ *
+ * @param strings
+ * @return true if any are empty
+ */
+ public static boolean anyEmpty(final String... strings) {
+ if (strings != null) {
+ for (final String object : strings) {
+ if (StringUtils.isEmpty(object)) {
+ return true;
+ }
+ }
+ return strings.length == 0 ? true : false;
+ }
+ return true;
+ }
+
+
+ /**
+ * Does equalsIgnoreCase call but if the value is '*', immediately returns true.
+ */
+ public static boolean equalsIgnoreCaseOrValueIsWildcard(final String value, final String toCheck) {
+ if (value == null && toCheck == null) {
+ return true;
+ }
+ if (value != null && StringUtil.WILDCARD.equals(value)) {
+ return true;
+ }
+ if (value != null && toCheck != null) {
+ return value.equalsIgnoreCase(toCheck);
+ }
+ return false;
+ }
}
Deleted: trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java
===================================================================
--- trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java 2010-03-30 11:06:50 UTC (rev 334)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/Sum.java 2010-03-30 22:18:44 UTC (rev 335)
@@ -1,156 +0,0 @@
-/*
- * 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;
-import java.math.BigDecimal;
-
-/**
- * Mutable class representing a sum of BigDecimals.
- * @author Benoit Xhenseval
- *
- */
-public class Sum implements Serializable {
- private static final long serialVersionUID = -8583271171731930344L;
- private BigDecimal value = BigDecimal.ZERO;
- private int count = 0;
-
- public Sum() {
- this(BigDecimal.ZERO, 2);
- }
-
- public Sum(final BigDecimal start) {
- if (start != null) {
- value = start;
- }
- }
-
- public Sum(final BigDecimal start, final int scale) {
- if (start != null) {
- value = start.setScale(scale, BigDecimal.ROUND_HALF_UP);
- }
- }
-
- public Sum(final Sum start) {
- if (start != null) {
- value = start.getTotal();
- }
- }
-
- public BigDecimal getTotal() {
- return value;
- }
-
- public void setTotal(final BigDecimal total) {
- if (total != null) {
- this.value = total;
- }
- }
-
- /**
- * @return the current Sum with new total.
- */
- public Sum add(final BigDecimal... value) {
- this.value = BigDecimalUtil.add(this.value, value);
- count += value.length;
- return this;
- }
-
- public Sum subtract(final BigDecimal... value) {
- this.value = BigDecimalUtil.subtract(this.value, value);
- count += value.length;
- return this;
- }
-
- public Sum add(final Integer value) {
- this.value = BigDecimalUtil.add(this.value, new BigDecimal(value));
- count++;
- return this;
- }
-
- /**
- * @return the current Sum with new total.
- */
- public Sum add(final Sum value) {
- if (value != null) {
- this.value = BigDecimalUtil.add(this.value, value.getTotal());
- count++;
- }
- return this;
- }
-
- /**
- * @return the current Sum with new total.
- */
- public Sum minus(final BigDecimal... value) {
- this.value = BigDecimalUtil.subtract(this.value, value);
- count += value.length;
- return this;
- }
-
- /**
- * @return the current Sum with new total.
- */
- public Sum minus(final Sum value) {
- if (value != null) {
- this.value = BigDecimalUtil.subtract(this.value, value.getTotal());
- count++;
- }
- return this;
- }
-
- @Override
- public String toString() {
- return value.toString();
- }
-
- public boolean isZero() {
- return BigDecimalUtil.isZero(value);
- }
-
- public boolean isNotZero() {
- return !isZero();
- }
-
- public boolean isNegative() {
- return BigDecimalUtil.isNegative(value);
- }
-
- public boolean isZeroOrLess() {
- return BigDecimalUtil.isZeroOrLess(value);
- }
-
- public int getCount() {
- return count;
- }
-}
Added: trunk/utils/src/main/java/net/objectlab/kit/util/Total.java
===================================================================
--- trunk/utils/src/main/java/net/objectlab/kit/util/Total.java (rev 0)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/Total.java 2010-03-30 22:18:44 UTC (rev 335)
@@ -0,0 +1,156 @@
+/*
+ * 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;
+import java.math.BigDecimal;
+
+/**
+ * Mutable class representing a sum of BigDecimals.
+ * @author Benoit Xhenseval
+ *
+ */
+public class Total implements Serializable {
+ private static final long serialVersionUID = -8583271171731930344L;
+ private BigDecimal value = BigDecimal.ZERO;
+ private int count = 0;
+
+ public Total() {
+ this(BigDecimal.ZERO, 2);
+ }
+
+ public Total(final BigDecimal start) {
+ if (start != null) {
+ value = start.setScale(2);
+ }
+ }
+
+ public Total(final BigDecimal start, final int scale) {
+ if (start != null) {
+ value = start.setScale(scale, BigDecimal.ROUND_HALF_UP);
+ }
+ }
+
+ public Total(final Total start) {
+ if (start != null) {
+ value = start.getTotal();
+ }
+ }
+
+ public BigDecimal getTotal() {
+ return value;
+ }
+
+ public void setTotal(final BigDecimal total) {
+ if (total != null) {
+ this.value = total;
+ }
+ }
+
+ /**
+ * @return the current Sum with new total.
+ */
+ public Total add(final BigDecimal... value) {
+ this.value = BigDecimalUtil.add(this.value, value);
+ count += value.length;
+ return this;
+ }
+
+ public Total subtract(final BigDecimal... value) {
+ this.value = BigDecimalUtil.subtract(this.value, value);
+ count += value.length;
+ return this;
+ }
+
+ public Total add(final Integer value) {
+ this.value = BigDecimalUtil.add(this.value, new BigDecimal(value));
+ count++;
+ return this;
+ }
+
+ /**
+ * @return the current Sum with new total.
+ */
+ public Total add(final Total value) {
+ if (value != null) {
+ this.value = BigDecimalUtil.add(this.value, value.getTotal());
+ count++;
+ }
+ return this;
+ }
+
+ /**
+ * @return the current Sum with new total.
+ */
+ public Total minus(final BigDecimal... value) {
+ this.value = BigDecimalUtil.subtract(this.value, value);
+ count += value.length;
+ return this;
+ }
+
+ /**
+ * @return the current Sum with new total.
+ */
+ public Total minus(final Total value) {
+ if (value != null) {
+ this.value = BigDecimalUtil.subtract(this.value, value.getTotal());
+ count++;
+ }
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return value.toString();
+ }
+
+ public boolean isZero() {
+ return BigDecimalUtil.isZero(value);
+ }
+
+ public boolean isNotZero() {
+ return !isZero();
+ }
+
+ public boolean isNegative() {
+ return BigDecimalUtil.isNegative(value);
+ }
+
+ public boolean isZeroOrLess() {
+ return BigDecimalUtil.isZeroOrLess(value);
+ }
+
+ public int getCount() {
+ return count;
+ }
+}
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-30 11:06:50 UTC (rev 334)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/WeightedAverage.java 2010-03-30 22:18:44 UTC (rev 335)
@@ -41,8 +41,8 @@
*/
public class WeightedAverage implements Serializable {
private static final long serialVersionUID = 4687472725716492770L;
- private final Sum total = new Sum();
- private final Sum totalExpanded = new Sum();
+ private final Total total = new Total();
+ private final Total totalExpanded = new Total();
private int count = 0;
public BigDecimal getTotal() {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <be...@us...> - 2010-04-07 21:07:20
|
Revision: 339
http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=339&view=rev
Author: benoitx
Date: 2010-04-07 21:07:11 +0000 (Wed, 07 Apr 2010)
Log Message:
-----------
Improve Utils.
Modified Paths:
--------------
trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java
Added Paths:
-----------
trunk/utils/src/main/java/net/objectlab/kit/util/Util.java
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-31 20:25:15 UTC (rev 338)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/StringUtil.java 2010-04-07 21:07:11 UTC (rev 339)
@@ -510,7 +510,7 @@
final StringBuilder b = new StringBuilder();
b.append(NEW_LINE);
final String line = StringUtils.repeat(String.valueOf(boxing), text.length() + 4);
- b.append(line);
+ b.append(line).append(NEW_LINE);
b.append(boxing).append(SPACE).append(text).append(SPACE).append(boxing).append(NEW_LINE);
b.append(line).append(NEW_LINE);
return b.toString();
Added: trunk/utils/src/main/java/net/objectlab/kit/util/Util.java
===================================================================
--- trunk/utils/src/main/java/net/objectlab/kit/util/Util.java (rev 0)
+++ trunk/utils/src/main/java/net/objectlab/kit/util/Util.java 2010-04-07 21:07:11 UTC (rev 339)
@@ -0,0 +1,186 @@
+/*
+ * 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.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import net.objectlab.kit.util.StringUtil;
+
+/**
+ * Utility class for list generation and parsing.
+ *
+ * @author Benoit Xhenseval
+ * @version $Revision: 1.3 $
+ */
+public final class Util {
+ /**
+ * default constructor.
+ */
+ private Util() {
+ }
+
+ /**
+ * helper method to convert a 'delimiter' separated string to a list.
+ *
+ * @param str
+ * the 'delimiter' separated string
+ * @param delimiter
+ * typically a ','
+ * @return a list
+ */
+ public static List<String> listify(final String str, final String delimiter) {
+ if (str == null) {
+ return Collections.emptyList();
+ }
+
+ final StringTokenizer tok = new StringTokenizer(str, delimiter);
+ final List<String> list = new ArrayList<String>();
+
+ while (tok.hasMoreElements()) {
+ list.add(StringUtil.trim(tok.nextToken()));
+ }
+
+ return list;
+ }
+
+ /**
+ * convert a list to a comma separated string.
+ *
+ * @param list
+ * list to "print"
+ * @return a String comma separated.
+ */
+ public static String listToCSVString(final List list) {
+ final StringBuffer buf = new StringBuffer();
+
+ if (list != null) {
+ boolean first = true;
+
+ for (final Iterator it = list.iterator(); it.hasNext(); first = false) {
+ if (!first) {
+ buf.append(",");
+ }
+ final Object obj = it.next();
+ if (obj != null) {
+ buf.append(obj.toString());
+ }
+ }
+ }
+
+ return buf.toString();
+ }
+
+ /**
+ * finds out the stack trace up to where the exception was thrown.
+ *
+ * @return String that contains the stack trace
+ */
+ public static String buildStackTraceString(final Throwable ex) {
+ final StringBuilder context = new StringBuilder(ex.toString());
+ final StringWriter sw = new StringWriter();
+ ex.printStackTrace(new PrintWriter(sw, true));
+ context.append('\n');
+ context.append(sw.toString());
+
+ return context.toString();
+ }
+
+ /**
+ * Finds information about the threads and dumps them into a String.
+ *
+ * @return the String containing info about all threads
+ */
+ public static String dumpThreads() {
+ final StringWriter sout = new StringWriter(); // Capture listing in a
+ // string
+ final PrintWriter out = new PrintWriter(sout);
+ Util.listAllThreads(out);
+ out.flush();
+
+ return sout.toString();
+ }
+
+ /** Find the root thread group and list it recursively */
+ private static void listAllThreads(final PrintWriter out) {
+ final ThreadGroup currentThreadGroup = Thread.currentThread().getThreadGroup();
+ ThreadGroup rootThreadGroup = currentThreadGroup;
+ ThreadGroup parent = rootThreadGroup.getParent();
+
+ while (parent != null) {
+ rootThreadGroup = parent;
+ parent = parent.getParent();
+ }
+
+ // And list it, recursively
+ Util.printGroupInfo(out, rootThreadGroup, "");
+ }
+
+ /** Display info about a thread group and its threads and groups */
+ private static void printGroupInfo(final PrintWriter out, final ThreadGroup group, final String indent) {
+ if (group == null) {
+ return;
+ }
+
+ final int numThreads = group.activeCount();
+ final int numGroups = group.activeGroupCount();
+ final Thread[] threads = new Thread[numThreads];
+ final ThreadGroup[] groups = new ThreadGroup[numGroups];
+ group.enumerate(threads, false);
+ group.enumerate(groups, false);
+ out.println(indent + "Thread Group: " + group.getName() + " Max Priority: " + group.getMaxPriority() + (group.isDaemon() ? " Daemon" : ""));
+
+ for (int i = 0; i < numThreads; i++) {
+ Util.printThreadInfo(out, threads[i], indent + " ");
+ }
+
+ for (int i = 0; i < numGroups; i++) {
+ Util.printGroupInfo(out, groups[i], indent + " ");
+ }
+ }
+
+ private static void printThreadInfo(final PrintWriter out, final Thread thread, final String indent) {
+ if (thread == null) {
+ return;
+ }
+
+ out.println(indent + "Thread: " + thread.getName() + " Priority: " + thread.getPriority() + (thread.isDaemon() ? " Daemon" : "")
+ + (thread.isAlive() ? "" : " Not Alive") + (thread.isInterrupted() ? " Interrupted" : ""));
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|