4555 |
02 Oct 08 |
nicklas |
1 |
/** |
4555 |
02 Oct 08 |
nicklas |
$Id$ |
4555 |
02 Oct 08 |
nicklas |
3 |
|
4555 |
02 Oct 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4555 |
02 Oct 08 |
nicklas |
5 |
|
4555 |
02 Oct 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4555 |
02 Oct 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4555 |
02 Oct 08 |
nicklas |
8 |
|
4555 |
02 Oct 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4555 |
02 Oct 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4555 |
02 Oct 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4555 |
02 Oct 08 |
nicklas |
of the License, or (at your option) any later version. |
4555 |
02 Oct 08 |
nicklas |
13 |
|
4555 |
02 Oct 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4555 |
02 Oct 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4555 |
02 Oct 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4555 |
02 Oct 08 |
nicklas |
GNU General Public License for more details. |
4555 |
02 Oct 08 |
nicklas |
18 |
|
4555 |
02 Oct 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4555 |
02 Oct 08 |
nicklas |
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 |
A utility class for working with {@link Unit}:s. |
4555 |
02 Oct 08 |
nicklas |
38 |
|
4555 |
02 Oct 08 |
nicklas |
@author Nicklas |
4555 |
02 Oct 08 |
nicklas |
@version 2.9 |
4555 |
02 Oct 08 |
nicklas |
@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 |
Pattern for checking if a string is a numeric value with an |
4555 |
02 Oct 08 |
nicklas |
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 |
Find the unit with a given symbol for a given quantity. Use |
4555 |
02 Oct 08 |
nicklas |
this method only when you need to find few units. Each invokation |
4555 |
02 Oct 08 |
nicklas |
results in a new database query. If you need to lookup mulitple units, |
4555 |
02 Oct 08 |
nicklas |
consider using a {@link UnitCache} instead. |
4555 |
02 Oct 08 |
nicklas |
57 |
|
4555 |
02 Oct 08 |
nicklas |
@param dc The DbControl to use for database access |
4555 |
02 Oct 08 |
nicklas |
@param quantityId The system ID of the quantity |
4555 |
02 Oct 08 |
nicklas |
@param symbol The unit symbol |
4555 |
02 Oct 08 |
nicklas |
@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 |
Splits a value with an optional unit. The input string |
4555 |
02 Oct 08 |
nicklas |
should start with a numerical value and optionally end |
4555 |
02 Oct 08 |
nicklas |
with a unit. If the value is not numeric, null is returned, |
4555 |
02 Oct 08 |
nicklas |
otherwise an array with two elements is returned. The 0th |
4555 |
02 Oct 08 |
nicklas |
element is the numeric part, and the 1st is the unit |
4555 |
02 Oct 08 |
nicklas |
part. The unit part is null if the value doesn't have a unit. |
4555 |
02 Oct 08 |
nicklas |
White-space has been stripped from all values. |
4555 |
02 Oct 08 |
nicklas |
@param value The value to split |
4555 |
02 Oct 08 |
nicklas |
@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 |
} |