src/core/net/sf/basedb/util/units/UnitUtil.java

Code
Comments
Other
Rev Date Author Line
4555 02 Oct 08 nicklas 1 /**
4555 02 Oct 08 nicklas 2   $Id$
4555 02 Oct 08 nicklas 3
4555 02 Oct 08 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4555 02 Oct 08 nicklas 5
4555 02 Oct 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4555 02 Oct 08 nicklas 7   Available at http://base.thep.lu.se/
4555 02 Oct 08 nicklas 8
4555 02 Oct 08 nicklas 9   BASE is free software; you can redistribute it and/or
4555 02 Oct 08 nicklas 10   modify it under the terms of the GNU General Public License
4555 02 Oct 08 nicklas 11   as published by the Free Software Foundation; either version 3
4555 02 Oct 08 nicklas 12   of the License, or (at your option) any later version.
4555 02 Oct 08 nicklas 13
4555 02 Oct 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4555 02 Oct 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4555 02 Oct 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4555 02 Oct 08 nicklas 17   GNU General Public License for more details.
4555 02 Oct 08 nicklas 18
4555 02 Oct 08 nicklas 19   You should have received a copy of the GNU General Public License
4555 02 Oct 08 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4555 02 Oct 08 nicklas 21 */
4555 02 Oct 08 nicklas 22 package net.sf.basedb.util.units;
4555 02 Oct 08 nicklas 23
4555 02 Oct 08 nicklas 24 import java.util.List;
4555 02 Oct 08 nicklas 25 import java.util.regex.Matcher;
4555 02 Oct 08 nicklas 26 import java.util.regex.Pattern;
4555 02 Oct 08 nicklas 27
4555 02 Oct 08 nicklas 28 import net.sf.basedb.core.DbControl;
4555 02 Oct 08 nicklas 29 import net.sf.basedb.core.ItemQuery;
4555 02 Oct 08 nicklas 30 import net.sf.basedb.core.Unit;
4555 02 Oct 08 nicklas 31 import net.sf.basedb.core.query.Expressions;
4555 02 Oct 08 nicklas 32 import net.sf.basedb.core.query.Hql;
4555 02 Oct 08 nicklas 33 import net.sf.basedb.core.query.Restrictions;
4555 02 Oct 08 nicklas 34 import net.sf.basedb.util.Values;
4555 02 Oct 08 nicklas 35
4555 02 Oct 08 nicklas 36 /**
4555 02 Oct 08 nicklas 37   A utility class for working with {@link Unit}:s.
4555 02 Oct 08 nicklas 38
4555 02 Oct 08 nicklas 39   @author Nicklas
4555 02 Oct 08 nicklas 40   @version 2.9
4555 02 Oct 08 nicklas 41   @base.modified $Date$
4555 02 Oct 08 nicklas 42 */
4555 02 Oct 08 nicklas 43 public class UnitUtil
4555 02 Oct 08 nicklas 44 {
4555 02 Oct 08 nicklas 45   /**
4555 02 Oct 08 nicklas 46     Pattern for checking if a string is a numeric value with an
4555 02 Oct 08 nicklas 47     optional unit. Used by {@link #splitValueWithUnit(String)}
4555 02 Oct 08 nicklas 48   */
4555 02 Oct 08 nicklas 49   public static final Pattern VALUE_WITH_UNIT = 
4555 02 Oct 08 nicklas 50     Pattern.compile("\\s*([+-]?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)\\.?\\s*(.*)");
4555 02 Oct 08 nicklas 51   
4555 02 Oct 08 nicklas 52   /**
4555 02 Oct 08 nicklas 53     Find the unit with a given symbol for a given quantity. Use
4555 02 Oct 08 nicklas 54     this method only when you need to find few units. Each invokation
4555 02 Oct 08 nicklas 55     results in a new database query. If you need to lookup mulitple units,
4555 02 Oct 08 nicklas 56     consider using a {@link UnitCache} instead.
4555 02 Oct 08 nicklas 57
4555 02 Oct 08 nicklas 58     @param dc The DbControl to use for database access
4555 02 Oct 08 nicklas 59     @param quantityId The system ID of the quantity
4555 02 Oct 08 nicklas 60     @param symbol The unit symbol
4555 02 Oct 08 nicklas 61     @return A unit object, or null if no unit is found
4555 02 Oct 08 nicklas 62   */
4555 02 Oct 08 nicklas 63   public static Unit getUnit(DbControl dc, String quantityId, String symbol)
4555 02 Oct 08 nicklas 64   {
4555 02 Oct 08 nicklas 65     ItemQuery<Unit> query = Unit.getQuery();
4555 02 Oct 08 nicklas 66     query.join(Hql.innerJoin("symbols", "smb"));
4555 02 Oct 08 nicklas 67     query.restrict(Restrictions.eq(Hql.property("quantity.systemId"), Expressions.string(quantityId)));
4555 02 Oct 08 nicklas 68     query.restrict(Restrictions.eq(Hql.property("smb", "symbol"), Expressions.string(symbol)));
4555 02 Oct 08 nicklas 69     List<Unit> list = query.list(dc);
4555 02 Oct 08 nicklas 70     return list.isEmpty() ? null : list.get(0);
4555 02 Oct 08 nicklas 71   }
4555 02 Oct 08 nicklas 72   
4555 02 Oct 08 nicklas 73   /**
4555 02 Oct 08 nicklas 74     Splits a value with an optional unit. The input string 
4555 02 Oct 08 nicklas 75     should start with a numerical value and optionally end
4555 02 Oct 08 nicklas 76     with a unit. If the value is not numeric, null is returned,
4555 02 Oct 08 nicklas 77     otherwise an array with two elements is returned. The 0th
4555 02 Oct 08 nicklas 78     element is the numeric part, and the 1st is the unit 
4555 02 Oct 08 nicklas 79     part. The unit part is null if the value doesn't have a unit.
4555 02 Oct 08 nicklas 80     White-space has been stripped from all values.
4555 02 Oct 08 nicklas 81     @param value The value to split
4555 02 Oct 08 nicklas 82     @return An array of length 2, or null
4555 02 Oct 08 nicklas 83   */
4555 02 Oct 08 nicklas 84   public static String[] splitValueWithUnit(String value)
4555 02 Oct 08 nicklas 85   {
4711 18 Dec 08 nicklas 86     if (value == null) return null;
4555 02 Oct 08 nicklas 87     String[] result = null;
4555 02 Oct 08 nicklas 88     Matcher m = VALUE_WITH_UNIT.matcher(value);
4555 02 Oct 08 nicklas 89     if (m.matches())
4555 02 Oct 08 nicklas 90     {
4555 02 Oct 08 nicklas 91       result = new String[2];
4555 02 Oct 08 nicklas 92       result[0] = m.group(1);
4555 02 Oct 08 nicklas 93       result[1] = Values.getStringOrNull(m.group(2));
4555 02 Oct 08 nicklas 94     }
4555 02 Oct 08 nicklas 95     return result;
4555 02 Oct 08 nicklas 96   }
4555 02 Oct 08 nicklas 97 }