Example usage for java.lang String compareTo

List of usage examples for java.lang String compareTo

Introduction

In this page you can find the example usage for java.lang String compareTo.

Prototype

public int compareTo(String anotherString) 

Source Link

Document

Compares two strings lexicographically.

Usage

From source file:ColumnExample.java

protected int sortedIndexOf(TableColumn tc) {
    // just do a linear search for now
    int stop = getColumnCount();
    String name = tc.getHeaderValue().toString();

    for (int i = 0; i < stop; i++) {
        if (name.compareTo(getColumn(i).getHeaderValue().toString()) <= 0) {
            return i;
        }//w w  w. j a va2s.c o  m
    }
    return stop;
}

From source file:br.ufc.deti.ecgweb.domain.security.LoginService.java

public boolean hasAccess(String login, String key) {

    if (login.compareTo("admin") == 0) {
        if (key.compareTo("qpalzm") == 0) {
            return true;
        }//from   w  ww.j  a va  2s.  c o  m
    }

    Login login_ = loginRepository.findByLogin(login);
    if (login_.getUuid().compareTo(key) != 0) {
        return false;
    }

    return true;
}

From source file:com.capitati.omtc.core.negotiation.SemanticVersionComparator.java

/**
 * Compare two components from the pre-release and build string arrays as
 * detailed in the Semantic Versioning 2.0.0-rc.1 specification.
 * //from  w  w w.  j ava  2s  .  c  o  m
 * @param oneComp - first value.
 * @param twoComp - second value.
 * @return If first value is determined to be less than the second value then
 * return -1. If one value is determined to be greater than the second value
 * then return 1. Otherwise, 0 is returned.
 * @see <a href="http://semver.org/">Semantic Versioning 2.0.0-rc.1</a>
 */
private static int componentCompare(final String oneComp, final String twoComp) {
    if ((oneComp == null) && (twoComp == null)) {
        return 0;
    }

    if ((oneComp == null) && (twoComp != null)) {
        return -1;
    }

    if ((oneComp != null) && (twoComp == null)) {
        return 1;
    }

    boolean isOneInt = false;
    boolean isTwoInt = false;
    int oneInt = 0;
    int twoInt = 0;

    try {
        oneInt = Integer.parseInt(oneComp);
        isOneInt = true;
    } catch (final NumberFormatException ex) {
        isOneInt = false;
    }

    try {
        twoInt = Integer.parseInt(twoComp);
        isTwoInt = true;
    } catch (final NumberFormatException ex) {
        isTwoInt = false;
    }

    final int comp = ((isOneInt == true) && (isTwoInt == true)) ? oneInt - twoInt : oneComp.compareTo(twoComp);
    final int compValue = (comp > 0) ? 1 : (comp < 0) ? -1 : 0;

    return compValue;
}

From source file:com.att.aro.core.util.Util.java

/**<pre>
 * Sorts domain numbers and names.//from  w  w  w  .j  av  a2s. c o  m
 *  matches on ###.###.###.### where # is any number.
 *  then builds a double based on address to compare.
 *  if alpha is involved then alpha comparison is used.
 * 
 * @return
 */
public static Comparator<String> getDomainSorter() {
    if (comparator == null) {
        comparator = new Comparator<String>() {
            Pattern pattern = Pattern.compile("([0-9]*)\\.([0-9]*)\\.([0-9]*)\\.([0-9]*)");

            @Override
            public int compare(String o1, String o2) {
                if (pattern != null && pattern.matcher(o1).find() && pattern.matcher(o2).find()) {
                    return getDomainVal(o1).compareTo(getDomainVal(o2));
                }
                return o1.compareTo(o2);
            }

            /**
             * Convert xxx.xxx.xxx.xxx to a Double
             *
             * @param domain
             * @return
             */
            private Double getDomainVal(String domain) {
                Double val = 0D;
                String[] temp = domain.split("\\.");
                if (temp != null) {
                    try {
                        for (int idx = 0; idx < temp.length; idx++) {
                            val = (val * 256) + Integer.valueOf(temp[idx]);
                        }
                    } catch (NumberFormatException e) {
                        val = 0D;
                    }
                }
                return val;
            }
        };
    }
    return comparator;
}

From source file:gemlite.core.internal.index.compare.ComparatorImpl.java

private int compareString(String o1, String o2) {
    return o1.compareTo(o2);
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.DayOfQuarter.java

public Object calculate(Map<String, Object> row) throws CalculationException, PrefixAndSuffixException {
    Date d = date.getValue(row);/* www. j  a  v  a  2 s.c o  m*/
    SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
    String s = sdf.format(d);
    if (s.compareTo("0321") < 0) {
        // 12 1 2
        if (IsLeapYear.isLeapYear(d)) {
            return 91;
        } else {
            return 90;
        }
    } else if (s.compareTo("1221") >= 0) {
        // 12 1 2
        if (IsLeapYear.isLeapYear(DateUtils.addYears(d, 1))) {
            return 91;
        } else {
            return 90;
        }
    } else if (s.compareTo("0921") >= 0) {
        // 9 10 11
        return 91;
    } else if (s.compareTo("0621") >= 0) {
        // 6 7 8
        return 92;
    } else {
        // 3 4 5
        return 92;
    }
}

From source file:de.topobyte.utilities.apache.commons.cli.DefaultOptionComparator.java

@Override
public int compare(Option opt1, Option opt2) {
    String o1 = OptionUtil.getKey(opt1);
    String o2 = OptionUtil.getKey(opt2);
    return o1.compareTo(o2);
}

From source file:org.nabucco.alfresco.enhScriptEnv.repo.script.registry.ClasspathRegisterableScript.java

/**
 * /*from w w w .j  a  v a  2  s. c o m*/
 * {@inheritDoc}
 */
@Override
public int compareTo(final RegisterableScript<ScriptLocation> o) {
    final ScriptLocation otherScriptLocation = o.getScriptInstance();
    final ScriptLocation scriptLocation = this.getScriptInstance();

    final String otherPath = otherScriptLocation.getPath();
    final String path = scriptLocation.getPath();
    return path.compareTo(otherPath);
}

From source file:io.awacs.demo.TestController.java

@RequestMapping(value = "/test/{id}", produces = "application/json")
public String test2(@PathVariable("id") String id) {
    if (id.compareTo("hello") >= 0)
        return bis1("test for throw exception");
    return new JSONObject().fluentPut("hello", bis1(id)).toJSONString();
}

From source file:com.jsfsample.util.StringUtil.java

/**
 * i.e  //from   w  w  w  .j  a  va 2s .  com
 *    academicYr = 1112, offsetYears = 1  => return 1213
 *    academicYr = 1112, offsetYears = -4 => return 0708
 *   academicYr = "", offsetYears = 1     => return ""
 *   academicYr = null, offsetYears = 1  => return "" 
 * 
 * @param academicYr
 * @param offsetYears
 * @return
 */
public static String calcAcademicYear(String academicYr, int offsetYears) {

    String rtnValue = null;
    String century1 = null;
    String century2 = null;

    if (StringUtil.isEmpty(academicYr))
        return "";

    if (!StringUtil.isEmpty(academicYr) && academicYr.trim().length() == 4) {
        if (academicYr.compareTo("9091") >= 0) {
            century1 = "19";
            if ("9900".equals(academicYr)) {
                century2 = "19";
            } else {
                century2 = "19";
            }
        } else {
            century1 = "20";
            century2 = "20";
        }

        String year1 = century1 + academicYr.substring(0, 2);
        String year2 = century2 + academicYr.substring(2);

        SimpleDateFormat df1 = new SimpleDateFormat("yyyy");
        SimpleDateFormat df2 = new SimpleDateFormat("yy");

        try {
            rtnValue = df2.format(DateUtil.addYear(df1.parse(year1), offsetYears))
                    + df2.format(DateUtil.addYear(df1.parse(year2), offsetYears));

            return rtnValue;
        } catch (ParseException e) {
            //should never happen
            throw new RuntimeException(e);
        }

    }

    return "";
}