Java String Length Get LengthConversionFactor(String OldUnits)

Here you can find the source of LengthConversionFactor(String OldUnits)

Description

Length Conversion Factor

License

Open Source License

Declaration

public static float LengthConversionFactor(String OldUnits) 

Method Source Code

//package com.java2s;
/*/*  w ww.j  a  v  a 2s  .c o m*/
 * File:  NxNodeUtils.java 
 *             
 * Copyright (C) 2001, Ruth Mikkelson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Ruth Mikkelson <mikkelsonr@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the Intense Pulsed Neutron Source Division
 * of Argonne National Laboratory, Argonne, IL 60439-4845, USA.
 *
 * For further information, see <http://www.pns.anl.gov/ISAW/>
 *
 * Modified:
 *
 * $Log$
 * Revision 1.25  2007/08/26 23:55:44  rmikk
 * Changed package name for the NeXus package
 *
 * Revision 1.24  2005/02/03 08:18:07  kramer
 *
 * Fixed an error in the getISO8601String(Date date) method, the current date
 * was used instead of 'date'.
 *
 * Commented out the section that writes fractions of a second because
 * parseISO8601(String dateString) can't read a ISO8601 string written to a
 * fraction of a second.
 *
 * Revision 1.23  2005/02/03 06:36:50  kramer
 *
 * Added the parseISO8601(String dateString) and getISO8601String(Date date)
 * methods which get a Date from an String written in ISO8601 format and a
 * String written in ISO8601 format from a Date.
 *
 * Revision 1.22  2004/12/23 18:47:05  rmikk
 * Fixed indentation and added lines between code.
 *
 * Revision 1.21  2004/05/14 15:03:27  rmikk
 * Removed unused variables
 *
 * Revision 1.20  2004/03/15 03:36:01  dennis
 * Moved view components, math and utils to new source tree
 * gov.anl.ipns.*
 *
 * Revision 1.19  2004/02/16 02:15:55  bouzekc
 * Removed unused import statements.
 *
 * Revision 1.18  2003/10/19 20:01:57  rmikk
 * Added some documentation
 * Fixed new units for time to be us
 * Fixed error in calculating factor with a numeric prefix
 *
 * Revision 1.17  2003/10/15 03:05:44  bouzekc
 * Fixed javadoc errors.
 *
 * Revision 1.16  2003/09/14 16:35:34  rmikk
 * -incorporated leading factors like 100us for times and
 * lengths
 * -Finished incorporating the time units up to hours.
 *
 * Revision 1.15  2003/06/18 20:32:54  pfpeterson
 * Removed deprecated method.
 *
 * Revision 1.14  2003/06/18 19:38:55  pfpeterson
 * ShowW() now calls method in StringUtil.
 *
 * Revision 1.13  2002/11/27 23:28:17  pfpeterson
 * standardized header
 *
 * Revision 1.12  2002/11/20 16:14:50  pfpeterson
 * reformating
 *
 * Revision 1.11  2002/06/19 16:27:04  rmikk
 * Eliminated commented out code.
 * Eliminated reference to deprecated Date.getYear()
 * Fixed code alignment and spacing
 *
 * Revision 1.10  2002/06/19 15:55:22  rmikk
 * Fixed the order of Date formats so that the seconds, when
 * there, is found
 *
 * Revision 1.9  2002/04/01 20:45:41  rmikk
 * Fixed Date Format exception report in jdk1.4
 * Added some support for the Nexus NXChar type
 *
 * Revision 1.8  2002/02/26 15:46:41  rmikk
 * Fixed the utility Showw routine
 * Added a utility routine to getConversion factors
 *
 */

public class Main {
    public static float LengthConversionFactor(String OldUnits) { //base m
        int n = getNumericStart(OldUnits);
        float factor = 1;

        if (n > 0)
            try {
                factor = (new Float(OldUnits.substring(0, n))).floatValue();

            } catch (Exception ss) {
                factor = 1;
            }
        OldUnits = OldUnits.substring(n);
        if ("m;meter;met;".indexOf(OldUnits + ";") >= 0)
            return factor * 1;

        if ("cm;centim;centimeter;cmeter;cmet;100mm;100millim;100millimeter;"
                .indexOf(OldUnits + ";") >= 0)
            return factor * .01f;

        if ("mm;millim;millimeter;100um;100microm;".indexOf(OldUnits + ";") >= 0)
            return factor * .001f;

        if ("um;umet;umeter;umeters;".indexOf(OldUnits) >= 0)
            return factor * .000001f;

        if ("in;inch;".indexOf(OldUnits + ";") >= 0)
            return factor * (float) (1.0 / 254.0);

        if ("ft;foot;feet;".indexOf(OldUnits + ";") >= 0)
            return factor * (float) (12.0 / 254.0);

        return factor * 1.0f;

    }

    private static int getNumericStart(String S) {

        boolean decimalDone = false, expOn = false, leadSign = true;

        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);

            if (Character.isDigit(c)) {
                leadSign = false;
            } else if ((c == '.') && !decimalDone && !expOn) {
                leadSign = false;
                decimalDone = true;

            } else if (("+-".indexOf(c) >= 0) && leadSign)
                leadSign = false;

            else if (("Ee".indexOf(c) >= 0) && !expOn) {
                expOn = true;

                leadSign = true;
                decimalDone = true;

            } else
                return i;

        }
        return S.length();
    }
}

Related

  1. length(String... args)
  2. lengthBetween(String param, int minLength, int maxLength)
  3. lengthByteInPrepared(String s)
  4. lengthCheck(final String str, final int minLength, final int maxLength)
  5. lengthConstToString(int val, int len)
  6. lengthExpandedTabs(String string, int toIdx, int tabWidth)
  7. lengthExpression(final String operand, final long length)
  8. lengthField(String propertyName)
  9. lengthFormat(String str, int maxLength)