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:com.jigsforjava.string.StringUtils.java

/**
 * Compares two String instances, allowing null in either or both strings.
 * If both strings are null, they are considered equal. If only one string
 * is null, the non-null string is considered greater. If both strings are
 * non-null, this method returns the results of string1.compareTo(string2).
 * /* w  ww  .j av  a  2s. com*/
 * @param string1 a String instance to compare. May be null.
 * @param string2 a String instance to compare. May be null.
 * @return <0 if string1 < string2, 0 if string1.equals(string2), >0 if
 *         string1 > string2.
 */
public static int compareTo(String string1, String string2) {
    int result = 0;

    if (string1 != null && string2 == null)
        result = 1;
    else if (string1 == null && string2 != null)
        result = -1;
    else if (string1 != null && string2 != null)
        result = string1.compareTo(string2);

    return result;
}

From source file:com.sfs.whichdoctor.formatter.TrainingStatusFormatter.java

public static String getField(final TrainingStatusBean status, final TrainingStatusAnalysisBean trainingStatus,
        final String field, final String format, final PreferencesBean preferences) {

    String value = "";

    if (status == null || field == null) {
        return value;
    }/*from   ww  w .j a  v  a  2 s.c  o m*/

    PersonBean person = new PersonBean();
    if (status.getPerson() != null) {
        person = status.getPerson();
    }

    if (field.compareTo("MIN") == 0) {
        value = String.valueOf(person.getPersonIdentifier());
    }
    if (field.compareTo("Person's Name") == 0) {
        value = OutputFormatter.toCasualName(person);
    }
    if (field.compareTo("Preferred Name") == 0) {
        value = person.getPreferredName();
    }
    if (field.compareTo("Last Name") == 0) {
        value = person.getLastName();
    }
    if (field.compareTo("Membership Status") == 0) {
        value = person.getMembershipField("Status");
    }
    if (field.compareTo("Division") == 0) {
        value = person.getMembershipField("Division");
    }
    if (field.compareTo("Membership Type") == 0) {
        value = person.getMembershipField("Membership Type");
    }
    if (field.compareTo("Training Type") == 0) {
        value = person.getMembershipField("Training Type");
    }

    if (field.compareTo("Training Program") == 0) {
        value = status.getTrainingOrganisation() + " - " + status.getTrainingProgram();
    }

    if (field.compareTo("Training Year") == 0) {
        value = String.valueOf(status.getYear());
    }

    if (field.compareTo("PREP/pre-PREP") == 0) {
        value = Formatter.convertBoolean(status.isPrep());
    }

    if (field.compareTo("Online Tools") == 0) {
        StringBuilder sb = new StringBuilder();

        if (status.getOnlineToolsStatus() != null) {
            for (String key : status.getOnlineToolsStatus().keySet()) {
                OnlineToolStatus ot = status.getOnlineToolsStatus().get(key);

                if (sb.length() > 0) {
                    if (StringUtils.equalsIgnoreCase(format, "html")) {
                        sb.append("<br />");
                    } else {
                        sb.append(", ");
                    }
                }

                sb.append(ot.getName());
                sb.append(": ");
                sb.append(ot.getCompletedCount());
                sb.append(" / ");
                sb.append(ot.getRequiredCount());
            }
        }
        value = sb.toString();
    }

    if (field.compareTo("AT: Interim Report(s) I") == 0) {
        value = getReportsByType(status, "Interim Report 1", format);
    }

    if (field.compareTo("AT: Final Supervisor Report(s)") == 0) {
        value = getReportsByType(status, "Final Report", format);
    }

    if (field.compareTo("AT: Trainee's Report(s)") == 0) {
        value = getReportsByType(status, "Trainee's Report (NZ)", format);
    }

    if (field.compareTo("BT: Mid-Year Progress Report(s)") == 0) {
        value = getReportsByType(status, "Mid Year Report", format);
    }

    if (field.compareTo("BT: Annual Progress Report(s)") == 0) {
        value = getReportsByType(status, "Annual Progress Report", format);
    }

    if (field.compareTo("Written Exams") == 0) {
        value = getExamsByType(status, "Written Exam", format);
    }

    if (field.compareTo("Clinical Exams") == 0) {
        value = getExamsByType(status, "Clinical Exam", format);
    }

    if (field.compareTo("Manual Assessments") == 0) {
        StringBuilder sb = new StringBuilder();

        if (status.getAssessmentsStatus() != null) {
            for (String key : status.getAssessmentsStatus().keySet()) {
                AssessmentStatus as = status.getAssessmentsStatus().get(key);

                if (sb.length() > 0) {
                    if (StringUtils.equalsIgnoreCase(format, "html")) {
                        sb.append("<br />");
                    } else {
                        sb.append(", ");
                    }
                }

                sb.append(as.getAssessmentType());
                if (StringUtils.isNotBlank(as.getProjectType())) {
                    sb.append(" - ");
                    sb.append(as.getProjectType());
                }
                sb.append(": ");
                sb.append(as.getSubmissionCount());
                sb.append(" / ");
                sb.append(as.getRequiredCount());
            }
        }
        value = sb.toString();
    }

    if (value == null) {
        value = "";
    }

    return value;
}

From source file:integratedtoolkit.util.OptimisComponents.java

private static String getResourceName(String IPv4, String location) {
    if (location.compareTo("") == 0) {
        return IPv4;
    } else {/*from  w  ww  .  j  ava2 s  . c o m*/
        return "http://" + IPv4 + location;
    }
}

From source file:de.tub.av.pe.xcapsrv.XMLValidator.java

public static boolean weaklyEquals(String xml1, String xml2) {

    // clean xml1 string
    xml1 = xml1.trim().replaceAll("\n", "").replaceAll("\t", "").replaceAll("\n", "").replaceAll("\r", "")
            .replaceAll("\f", "");

    // clean xml2 string
    xml2 = xml2.trim().replaceAll("\n", "").replaceAll("\t", "").replaceAll("\n", "").replaceAll("\r", "")
            .replaceAll("\f", "");

    return xml1.compareTo(xml2) == 0;

}

From source file:com.karura.framework.plugins.Capture.java

/**
 * Queries the media store to find out what the file path is for the Uri we supply
 * /* w  ww .  j  a v a  2 s . com*/
 * @param contentUri
 *            the Uri of the audio/image/video
 * @param context
 *            the current application context
 * @return the full path to the file
 */
@SuppressWarnings("deprecation")
public static String getRealPathFromURI(Uri contentUri, Activity context) {
    final String scheme = contentUri.getScheme();

    if (scheme == null) {
        return contentUri.toString();
    } else if (scheme.compareTo("content") == 0) {
        String[] proj = { DATA };
        Cursor cursor = context.managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else if (scheme.compareTo("file") == 0) {
        return contentUri.getPath();
    } else {
        return contentUri.toString();
    }
}

From source file:de.hadesrofl.json.JsonReader.java

/**
 * Reads a json file and returns it as JSONObject
 *
 * @param file// w ww.java2  s. co m
 *            is the file to read
 * @return an json object
 */
public static JSONObject readFile(File f) {
    String jsonData = "";
    BufferedReader br = null;
    try {
        String line;
        br = new BufferedReader(new FileReader(f));
        while ((line = br.readLine()) != null) {
            jsonData += line + "\n";
        }
    } catch (IOException e) {
        System.err.println("Can't read json file!");
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
            System.err.println("Can't close buffered reader!");
        }
    }
    JSONObject obj = null;
    if (jsonData.compareTo("") != 0)
        obj = new JSONObject(jsonData);
    return obj;
}

From source file:com.justgiving.raven.kissmetrics.utils.KissmetricsLocalSchemaExtractor.java

/****
 * This function counts the total occurrences of keys, in the json records files
 *  //from  w w w  .java2  s.  c  o m
 * @param input_filename path to a json file
 * @return a HashMap with the keys / total counts pairs
 */
private static KeyValueCounter countKeysInJsonRecordsFile(String input_filename) {
    InputStream fis;
    BufferedReader bufferedReader;
    String line;
    JSONParser jsonParser = new JSONParser();
    KeyValueCounter keyValueCounter = new KeyValueCounter();
    String jsonValue = "";
    try {
        fis = new FileInputStream(input_filename);
        bufferedReader = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
        while ((line = bufferedReader.readLine()) != null) {
            JSONObject jsonObject = (JSONObject) jsonParser.parse(line);
            Set<String> keyset = jsonObject.keySet();
            for (String jsonkey : keyset) {
                if (jsonObject.get(jsonkey) != null) {
                    jsonValue = (String) jsonObject.get(jsonkey).toString();
                    if (jsonValue == null || jsonValue == "") {
                        jsonValue = "";
                    }
                    int lenValue = jsonValue.length();
                    keyValueCounter.incrementKeyCounter(jsonkey);
                    keyValueCounter.putValueLength(jsonkey, lenValue);
                } else {
                    if (jsonkey.compareTo("user_agent") != 0) {
                        logger.error("Errot typing to get jsonkey " + jsonkey);
                    }

                }
            }
        }
        bufferedReader.close();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    //System.out.println(keyCounter.toString());
    //System.out.println(sortHashByKey(keyCounter));      
    return keyValueCounter;
}

From source file:com.clustercontrol.accesscontrol.factory.RoleModifier.java

/**
 * ?<BR>/*from w w  w  .j av a2 s  .  c  om*/
 * 
 * @param roleId ?ID
 * @param modifyUserId ID
 * @throws RoleNotFound
 * @throws UnEditableRole
 * @throws UsedRole
 * @throws HinemosUnknown
 */
public static void deleteRoleInfo(String roleId, String modifyUserId)
        throws RoleNotFound, UnEditableRole, UsedRole, HinemosUnknown {
    if (roleId != null && roleId.compareTo("") != 0 && modifyUserId != null
            && modifyUserId.compareTo("") != 0) {
        HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

        try {
            // ????
            RoleInfo role = QueryUtil.getRolePK(roleId);
            // ????
            if (role != null && !role.getRoleType().equals(RoleTypeConstant.USER_ROLE)) {
                throw new UnEditableRole();
            }
            if (role.getUserInfoList() != null && role.getUserInfoList().size() > 0) {
                throw new UsedRole();
            }

            // ?
            role.unchainUserInfoList();
            role.unchainSystemPrivilegeInfoList();
            // ?
            em.remove(role);

        } catch (RoleNotFound | UnEditableRole | UsedRole e) {
            throw e;
        } catch (Exception e) {
            m_log.warn("deleteRoleInfo() failure to delete a role. (roleId = " + roleId + ")", e);
            throw new HinemosUnknown(e.getMessage(), e);
        }

        m_log.info("successful in deleting a role. (roleId = " + roleId + ")");
    }
}

From source file:com.sfs.whichdoctor.formatter.RotationFormatter.java

public static String getField(final RotationBean rotation, final Object object, final String section,
        final String field, final String format) {

    String value = "";

    if (section == null) {
        return value;
    }//from w ww . j  a  v a  2s  . com
    if (field == null) {
        return value;
    }
    if (field.compareTo("GUID") == 0) {
        value = String.valueOf(rotation.getGUID());
    }
    if (field.compareTo("MIN") == 0) {
        if (rotation.getPerson() != null) {
            value = String.valueOf(rotation.getPerson().getPersonIdentifier());
        }
    }

    if (field.compareTo("Title") == 0) {
        if (rotation.getPerson() != null) {
            PersonBean person = rotation.getPerson();
            value = person.getTitle();
        }
    }
    if (field.compareTo("Preferred Name") == 0) {
        if (rotation.getPerson() != null) {
            PersonBean person = rotation.getPerson();
            value = person.getPreferredName();
        }
    }
    if (field.compareTo("Last Name") == 0) {
        if (rotation.getPerson() != null) {
            PersonBean person = rotation.getPerson();
            value = person.getLastName();
        }
    }
    if (field.compareTo("Formatted Name") == 0) {
        if (rotation.getPerson() != null) {
            PersonBean person = rotation.getPerson();
            value = OutputFormatter.toFormattedName(person);
        }
    }

    if (field.compareTo("Person") == 0) {
        if (rotation.getPerson() != null) {
            PersonBean person = rotation.getPerson();
            value = OutputFormatter.toCasualName(person);
        }
    }

    if (field.compareTo("Type") == 0) {
        value = rotation.getRotationType();
    }
    if (field.compareTo("Description") == 0) {
        value = rotation.getDescription();
    }
    if (field.compareTo("Training Year") == 0 && rotation.getYear() > 0) {
        value = String.valueOf(rotation.getYear());
    }
    if (field.compareTo("Starting Date") == 0) {
        if (rotation.getStartDate() != null) {
            value = Formatter.conventionalDate(rotation.getStartDate());
        }
    }
    if (field.compareTo("Completion Date") == 0) {
        if (rotation.getEndDate() != null) {
            value = Formatter.conventionalDate(rotation.getEndDate());
        }
    }
    if (field.compareTo("Training Time") == 0) {
        value = String.valueOf(rotation.getTrainingTime() * 100) + "%";
    }

    if (section.compareTo("Accreditations") == 0) {
        AccreditationBean accreditation = (AccreditationBean) object;

        if (field.compareTo("Accreditation Type") == 0) {
            value = accreditation.getAccreditationType();
        }
        if (field.compareTo("Accreditation Specialty") == 0) {
            value = accreditation.getSpecialtyType();
        }
        if (field.compareTo("Accreditation Sub-Specialty") == 0) {
            value = accreditation.getSpecialtySubType();
        }
        if (field.compareTo("Weeks Approved") == 0) {
            value = String.valueOf(accreditation.getWeeksApproved());
        }
        if (field.compareTo("Weeks Certified") == 0) {
            value = String.valueOf(accreditation.getWeeksCertified());
        }
        if (field.compareTo("Core Accreditation") == 0) {
            value = Formatter.convertBoolean(accreditation.getCore());
        }
    }

    if (section.compareTo("Reports") == 0) {
        ReportBean report = (ReportBean) object;

        if (field.compareTo("Report Type") == 0) {
            value = report.getReportType();
        }
        if (field.compareTo("Report Status") == 0) {
            value = report.getReportStatus();
        }
        if (field.compareTo("Report Authors") == 0 && report.getAuthors() != null) {
            StringBuilder authors = new StringBuilder();

            for (PersonBean person : report.getAuthors()) {
                if (authors.length() > 0) {
                    authors.append(", ");
                }
                authors.append(OutputFormatter.toFormattedName(person));
            }
            value = authors.toString();
        }
    }

    if (section.compareTo("Memos") == 0) {
        MemoBean memo = (MemoBean) object;

        if (field.compareTo("Type") == 0) {
            value = memo.getType();
        }
        if (field.compareTo("Date Created") == 0) {
            value = Formatter.conventionalDate(memo.getCreatedDate());
        }
        if (field.compareTo("Date Expires") == 0) {
            value = Formatter.conventionalDate(memo.getExpires());
        }
        if (field.compareTo("Message") == 0) {
            value = memo.getMessage();
        }
    }

    if (value == null) {
        value = "";
    }

    return value;
}

From source file:Main.java

/**
 * Compares two absolute URI TNF records
 *
 * @param[in] pString1 The string corresponding to the first TNF record
 * @param[in] pString2 The string corresponding to the second TNF record
 * @return true if the TNF records are equal
 *///from  w  w  w.  j av a  2  s  .c  om
/* package protected */static boolean compareAbsoluteURI(String pString1, String pString2) {

    if (pString1 == null) {
        if (pString2 == null) {
            return (true);
        }
        return (false);
    }

    String pTempString1 = convertStringToPrintableString(pString1);
    String pTempString2 = convertStringToPrintableString(pString2);

    if (pTempString1 != null && pTempString2 != null) {
        if (pTempString1.length() != pTempString2.length())
            return false;
    } else {
        return false;
    }

    if (pTempString1.length() == 0) {
        return true;
    }

    pTempString1 = normalizeAbsoluteURI(pTempString1);
    pTempString2 = normalizeAbsoluteURI(pTempString2);

    return (pTempString1.compareTo(pTempString2) == 0) ? true : false;
}