Example usage for org.apache.commons.lang StringUtils substring

List of usage examples for org.apache.commons.lang StringUtils substring

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils substring.

Prototype

public static String substring(String str, int start, int end) 

Source Link

Document

Gets a substring from the specified String avoiding exceptions.

Usage

From source file:su.comp.bk.ui.BkEmuActivity.java

protected void doFinishBinImageLoad(boolean isImageLoadedSuccessfully) {
    // Set result in parameters block
    if (isImageLoadedSuccessfully) {
        synchronized (computer) {
            int tapeParamsBlockAddrNameIdx;
            if (!computer.getConfiguration().isMemoryManagerPresent()) { // BK0010
                tapeParamsBlockAddrNameIdx = 26;
                // Set "OK" result code
                computer.writeMemory(true, tapeParamsBlockAddr + 1, 0);
                // Write loaded image start address
                computer.writeMemory(false, tapeParamsBlockAddr + 22, lastBinImageAddress);
                // Write loaded image length
                computer.writeMemory(false, tapeParamsBlockAddr + 24, lastBinImageLength);
                // Return from EMT 36
                computer.getCpu().returnFromTrap(false);
            } else { // BK0011
                tapeParamsBlockAddrNameIdx = 28;
                // Set "OK" result code
                computer.getCpu().clearPswFlagC();
                // Write loaded image start address
                computer.writeMemory(false, tapeParamsBlockAddr + 24, lastBinImageAddress);
                // Write loaded image length
                computer.writeMemory(false, tapeParamsBlockAddr + 26, lastBinImageLength);
                // Return from tape load routine
                computer.getCpu().writeRegister(false, Cpu.PC, computer.getCpu().pop());
            }//from  ww  w  .java 2  s  .co  m
            // Write loaded image name
            String tapeFileName = StringUtils.substringAfterLast(lastBinImageFileUri, "/");
            tapeFileName = StringUtils.substring(tapeFileName, 0, MAX_TAPE_FILE_NAME_LENGTH);
            byte[] tapeFileNameBuffer;
            try {
                tapeFileNameBuffer = tapeFileName.getBytes("koi8-r");
            } catch (UnsupportedEncodingException e) {
                tapeFileNameBuffer = tapeFileName.getBytes();
            }
            byte[] tapeFileNameData = new byte[MAX_TAPE_FILE_NAME_LENGTH];
            Arrays.fill(tapeFileNameData, (byte) ' ');
            System.arraycopy(tapeFileNameBuffer, 0, tapeFileNameData, 0,
                    Math.min(tapeFileNameBuffer.length, MAX_TAPE_FILE_NAME_LENGTH));
            for (int idx = 0; idx < tapeFileNameData.length; idx++) {
                computer.getCpu().writeMemory(true, tapeParamsBlockAddr + tapeParamsBlockAddrNameIdx + idx,
                        tapeFileNameData[idx]);
            }
        }
    }
}

From source file:substring.Static.java

public static void main(String[] args) throws Throwable {

    Runtime rt = Runtime.getRuntime();
    double alloc = rt.totalMemory() / 1000.0;
    double free = rt.freeMemory() / 1000.0;

    System.out.printf("Allocated (before substring operation) : %.2f kb\nFree: %.2f kb\n\n", alloc, free);
    Scanner in = new Scanner(new File("my_file.txt"));
    List<String> al = new ArrayList<String>();

    while (in.hasNextLine()) {
        String s = in.nextLine();
        /* Problem Code */
        //al.add(s.substring(0, 200)); // extracts first 200 characters
        /* Solution Code */
        //al.add(new String(s.substring(0,200))); 
        /* Using static menthod */
        // al.add(substring(s, 0, 200));
        /* Using apache */
        al.add(StringUtils.substring(s, 0, 200));
    }//from  w ww.  j  av  a 2 s. co m

    alloc = rt.totalMemory() / 1000.0;
    free = rt.freeMemory() / 1000.0;
    System.out.printf("\nAllocated (after substring operation): %.2f kb\nFree: %.2f kb\n\n", alloc, free);

    in.close();
    System.gc();

    alloc = rt.totalMemory() / 1000.0;
    free = rt.freeMemory() / 1000.0;
    System.out.printf("\nAllocated (after System.gc() operation):: %.2f kb\nFree: %.2f kb\n\n", alloc, free);
}

From source file:technology.tikal.gae.service.template.RestControllerTemplate.java

private String getValidationArrayNumber(String param) {
    String result = "";
    int start = 0;
    int openingCount = 0;
    for (int index = 0; index < param.length(); index++) {
        if (openingCount > 0) {
            if (param.charAt(index) == ']') {
                openingCount = openingCount - 1;
                if (openingCount == 0) {
                    result = StringUtils.substring(param, start + 1, index);
                }//from   ww  w .jav  a 2s  . c  om
            }
            if (param.charAt(index) == '[') {
                openingCount = openingCount + 1;
            }
        } else {
            if (param.charAt(index) == '[') {
                start = index;
                openingCount = 1;
            }
        }
    }
    return result;
}

From source file:ubic.gemma.analysis.service.ExpressionDataFileServiceImpl.java

/**
 * Loads the probe to probe coexpression link information for a given expression experiment and writes it to disk.
 * //  ww w .j  a  va  2 s.  c  o m
 * @param file
 * @param ee
 * @throws IOException
 */
private void writeCoexpressionData(File file, ExpressionExperiment ee) throws IOException {

    Taxon tax = expressionExperimentService.getTaxon(ee);
    assert tax != null;
    Collection<ProbeLink> probeLinks = probe2ProbeCoexpressionService.getProbeCoExpression(ee,
            tax.getCommonName());

    Collection<ArrayDesign> arrayDesigns = expressionExperimentService.getArrayDesignsUsed(ee);
    Map<Long, String[]> geneAnnotations = this.getGeneAnnotationsAsStrings(arrayDesigns);

    Date timestamp = new Date(System.currentTimeMillis());
    StringBuffer buf = new StringBuffer();

    // Write header information
    buf.append("# Coexpression Data for:  " + ee.getShortName() + " : " + ee.getName() + " \n");
    buf.append("# Generated On: " + timestamp + " \n");
    buf.append(DISCLAIMER);
    if (geneAnnotations.isEmpty()) {
        log.info("Platform anotation File Missing for Experiment, unable to include annotation information");
        buf.append(
                "# The platform annotation file is missing for this Experiment, unable to include gene annotation information \n");
        buf.append("probeId_1 \t probeId_2 \t score \n");
    } else
        buf.append(
                "probe_1 \t gene_symbol_1 \t gene_name_1 \t probe_2 \t gene_symbol_2 \t gene_name_2 \t score \n");

    // Data
    for (ProbeLink link : probeLinks) {

        if (geneAnnotations.isEmpty()) {
            buf.append(link.getFirstDesignElementId() + "\t" + link.getSecondDesignElementId() + "\t");
        } else {
            String[] firstAnnotation = geneAnnotations.get(link.getFirstDesignElementId());
            String[] secondAnnotation = geneAnnotations.get(link.getSecondDesignElementId());

            buf.append(firstAnnotation[0] + "\t" + firstAnnotation[1] + "\t" + firstAnnotation[2] + "\t");
            buf.append(secondAnnotation[0] + "\t" + secondAnnotation[1] + "\t" + secondAnnotation[2] + "\t");
        }

        buf.append(StringUtils.substring(link.getScore().toString(), 0, 5) + "\n");
    }

    // Write coexpression data to file (zipped of course)
    Writer writer = new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(file)));
    writer.write(buf.toString());
    writer.flush();
    writer.close();

}

From source file:uk.ac.ebi.bioinvindex.model.Investigation.java

@Override
public String toString() {
    String studies = "";
    for (Study study : getStudies()) {
        studies += String.format("Study{ id = '%s', accession = '%s', title = '%s' }\n", study.getId(),
                study.getAcc(), study.getTitle());
    }//  w  w  w  .  ja v  a 2s .c  o m

    return String.format(
            "Investigation{ id = %s, accession = '%s', title = '%s', description = '%s', submitted = %s, released = %s, "
                    + "contacts = '%s', publications = %s, Xrefs = %s\n\n Studies: {\n %s \n  }\n}\n\n",
            getId(), getAcc(), getTitle(), StringUtils.substring(getDescription(), 0, 20), getSubmissionDate(),
            getReleaseDate(), getContacts(), getPublications(), getXrefs(), studies);

}

From source file:uk.ac.ebi.bioinvindex.model.Protocol.java

public String toString() {
    return "Protocol{" + "id=" + getId() + ", acc='" + getAcc() + '\'' + ", name='" + name + "'" + ", type="
            + type + ", uri='" + uri + "'" + ", version='" + version + "'" + ", description='"
            + StringUtils.substring(description, 0, 20) + '\'' + ", parameters=" + parameters + ", components="
            + components + '}';
}

From source file:uk.ac.ebi.bioinvindex.model.Study.java

@Override
public String toString() {

    String investigationsStr = "";
    for (Investigation investigation : investigations) {
        investigationsStr += String.format("Investigation{ id = '%s', accession = '%s', title = '%s' }",
                investigation.getId(), investigation.getAcc(), investigation.getTitle());
    }// ww  w.j  a  v a 2 s . c  om

    String assaysStr = "";

    {
        String separator = "";
        int count = 0;
        for (Assay assay : getAssays()) {
            assaysStr += String.format("%s{#%d, acc: '%s', ep: %s, tech %s, material = %s}", separator,
                    assay.getId(), assay.getAcc(), assay.getMeasurement(), assay.getTechnology(),
                    assay.getMaterial());
            separator = ", ";
            if (count++ > 10)
                break;
        }
    }

    String assayResultsStr = "";
    {
        String separator = "";
        int count = 0;
        for (AssayResult assay : getAssayResults()) {
            assaysStr += String.format("%s{#%d, FV: %s, data: %s}", separator, assay.getId(),
                    assay.getFactorValues(), assay.getData());
            separator = ", ";
            if (count++ > 10)
                break;
        }
    }

    return "Study{" + "id ='" + getId() + '\'' + ", acc ='" + getAcc() + '\'' + ", title='" + title + '\''
            + "\n description='" + StringUtils.substring(description, 0, 20) + '\'' + "\n objective='"
            + objective + '\'' + "\n designs=" + getDesigns() + ", submissionDate=" + submissionDate
            + ", releaseDate=" + releaseDate + "\n contacts=" + contacts + "\n first assays=[" + assaysStr + "]"
            + "\n assay-results=" + assayResultsStr + "\n publications=" + publications + "\n protocols="
            + protocols + "\n\n investigations={\n" + investigationsStr + "\n  }\n}\n" + "\n\n users={\n"
            + users + "\n  }\n}\n" + "\n}";
}

From source file:uk.ac.ebi.bioinvindex.persistence.StudyPersister.java

/**
 * Checks that the study accession is unique, throws an exception in case it isn't.
 * /* www.j a  v a 2s .c o m*/
 */
@Override
public Study persist(Study object) {
    if (object == null) {
        log.warn("WARNING: attempt to persist a null study, we will ignore this!");
        return null;
    }

    Study study = ((StudyDAO) dao).getByAcc(object.getAcc());
    if (study != null)
        throw new RuntimeException("The accession \"" + object.getAcc() + "\" (assigned to \""
                + StringUtils.substring(object.getTitle(), 0, 15)
                + "\") is already being used for another study in the" + " database (assigned to \""
                + StringUtils.substring(object.getTitle(), 0, 15) + "\"), please define another accession");
    return super.persist(object);
}