001
002/*******************************************************************************
003 * Crown Copyright (c) 2006 - 2014, Copyright (c) 2006 - 2014 Kestral Computing & Health Intersections.
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 * 
009 * Contributors:
010 *    Kestral Computing P/L - initial implementation
011 *    Health Intersections - ongoing maintenance
012 *******************************************************************************/
013
014package org.fhir.ucum;
015
016public class Utilities {
017  public static boolean isWhitespace(String s) {
018    boolean ok = true;
019    for (int i = 0; i < s.length(); i++)
020      ok = ok && Character.isWhitespace(s.charAt(i));
021    return ok;
022    
023  }
024
025  public static boolean isDecimal(String string) {
026    if (Utilities.noString(string))
027      return false;
028    try {
029      float r = Float.parseFloat(string);
030      return r != r + 1; // just to suppress the hint
031    } catch (Exception e) {
032      return false;
033    }
034  }
035
036  public static boolean isInteger(String string) {
037    try {
038      int i = Integer.parseInt(string);
039      return i != i+1;
040    } catch (Exception e) {
041      return false;
042    }
043  }
044  
045  public static boolean isHex(String string) {
046    try {
047      int i = Integer.parseInt(string, 16);
048      return i != i+1;
049    } catch (Exception e) {
050      return false;
051    }
052  }
053  
054
055  public static boolean noString(String v) {
056    return v == null || v.equals("");
057  }
058
059  public static String padLeft(String src, char c, int len) {
060    StringBuilder s = new StringBuilder();
061    for (int i = 0; i < len - src.length(); i++)
062      s.append(c);
063    s.append(src);
064    return s.toString();
065  }
066
067  public static boolean isAsciiChar(char ch) {
068    return ch >= ' ' && ch <= '~'; 
069  }
070
071
072
073}