Example usage for org.joda.time MutablePeriod MutablePeriod

List of usage examples for org.joda.time MutablePeriod MutablePeriod

Introduction

In this page you can find the example usage for org.joda.time MutablePeriod MutablePeriod.

Prototype

public MutablePeriod() 

Source Link

Document

Creates a zero-length period using the standard period type.

Usage

From source file:com.bjond.utilities.JodaTimeUtils.java

License:Open Source License

public static Period toPeriod(int... valueUnitPairs) throws Exception {
    if (valueUnitPairs.length == 0)
        throw new Exception("empty unit and value pair");

    if ((valueUnitPairs.length % 2) != 0)
        throw new Exception("unit and value must be specified in a pair (first unit, second value)");

    val p = new MutablePeriod();
    for (int i = 0; i < valueUnitPairs.length; i = i + 2) {
        val value = valueUnitPairs[i];
        val unit = valueUnitPairs[i + 1];
        if (value < 0)
            throw new Exception("Negative value: " + value);
        switch (unit) {
        case SECONDS:
            p.addSeconds(value);//from   w w w.  j  a  v a 2  s .  co m
            break;
        case MINUTES:
            p.addMinutes(value);
            break;
        case HOURS:
            p.addHours(value);
            break;
        case DAYS:
            p.addDays(value);
            break;
        case WEEKS:
            p.addWeeks(value);
            break;
        case MONTHS:
            p.addMonths(value);
            break;
        case YEARS:
            p.addYears(value);
            break;
        default:
            throw new Exception("Invalid unit: " + unit);
        }
    }

    return p.toPeriod();
}

From source file:com.bjond.utilities.JodaTimeUtils.java

License:Open Source License

public static Period toPeriodFromFloat(float value, int unit) throws Exception {
    if (unit == WEEKS)
        throw new Exception("Fractional weeks not supported");

    val ret = new MutablePeriod();
    do {//from  w  w  w.ja v a 2s  .c  om
        int whole = wholePart(value);
        float fractional = fractionalPart(value);
        val part = toPeriod(whole, unit);
        ret.add(part);
        unit--;
        if (unit == WEEKS)
            unit--; // skip weeks.
        if (unit < 0)
            continue;
        value = range(unit) * fractional;

    } while (unit >= 0);

    return ret.toPeriod();
}

From source file:de.tshw.worktracker.view.TimeEntriesTableModel.java

License:MIT License

public String update(WorkTracker workTracker) {
    int oldProjectCount = elapsedTimes.size();
    HashMap<Project, MutablePeriod> newTimes = new HashMap<>();
    for (Project p : workTracker.getProjects()) {
        newTimes.put(p, new MutablePeriod());
    }// w w  w  .j  a  va  2 s . c o m
    MutablePeriod totalTimeToday = new MutablePeriod();
    for (WorkLogEntry entry : workTracker.getTodaysWorkLogEntries()) {
        if (!newTimes.containsKey(entry.getProject())) {
            newTimes.put(entry.getProject(), new MutablePeriod());
        }
        if (entry.getStartTime().toLocalDate().toDateTimeAtStartOfDay()
                .equals(LocalDate.now().toDateTimeAtStartOfDay())) {
            newTimes.get(entry.getProject()).add(entry.getTimeElapsed());
            if (!entry.getProject().equals(workTracker.getPauseProject())) {
                totalTimeToday.add(entry.getTimeElapsed());
            }
        }
    }
    WorkLogEntry entry = workTracker.getCurrentLogEntry();
    Period period = new Period(entry.getStartTime(), LocalDateTime.now());
    newTimes.get(entry.getProject()).add(period);
    if (!entry.getProject().equals(workTracker.getPauseProject())) {
        totalTimeToday.add(period);
    }

    for (Project p : newTimes.keySet()) {
        elapsedTimes.put(p, newTimes.get(p).toPeriod());
    }

    this.totalTimeElapsedToday = totalTimeToday.toPeriod();
    if (oldProjectCount == elapsedTimes.size()) {
        this.fireTableRowsUpdated(0, elapsedTimes.size());
    } else {
        this.fireTableDataChanged();
    }

    return periodFormatter.print(this.totalTimeElapsedToday.normalizedStandard());
}

From source file:io.warp10.script.functions.DURATION.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object o = stack.pop();/*from w w w .j a  va 2  s .c  om*/

    if (!(o instanceof String)) {
        throw new WarpScriptException(getName()
                + " expects an ISO8601 duration (a string) on top of the stack. See http://en.wikipedia.org/wiki/ISO_8601#Durations");
    }

    ReadWritablePeriod period = new MutablePeriod();

    ISOPeriodFormat.standard().getParser().parseInto(period, o.toString(), 0, Locale.US);

    Period p = period.toPeriod();

    if (p.getMonths() != 0 || p.getYears() != 0) {
        throw new WarpScriptException(getName()
                + " doesn't support ambiguous durations containing years or months, please convert those to days.");
    }

    Duration duration = p.toDurationFrom(new Instant());

    stack.push(duration.getMillis() * Constants.TIME_UNITS_PER_MS);

    return stack;
}

From source file:org.apache.tamaya.jodatime.PeriodConverter.java

License:Apache License

@Override
public Period convert(String value, ConversionContext context) {
    String trimmed = Objects.requireNonNull(value).trim();

    context.addSupportedFormats(PeriodConverter.class, "PyYmMwWdDThHmMsS");
    context.addSupportedFormats(PeriodConverter.class, "Pyyyy-mm-ddThh:mm:ss");

    MutablePeriod result = null;/*from www .  j  a v a 2 s  .  c  o m*/
    PeriodParser format = null;

    if (isISOFormat(trimmed)) {
        format = ISO_FORMAT;
    } else if (isAlternativeFormat(trimmed)) {
        format = ALTERNATIVE_FORMAT;
    }

    if (format != null) {
        result = new MutablePeriod();
        int parseResult = format.parseInto(result, trimmed, 0, Locale.ENGLISH);

        if (parseResult < 0) {
            result = null;
        }
    }

    return result != null ? result.toPeriod() : null;
}

From source file:org.phenotips.data.internal.controller.MedicationController.java

License:Open Source License

@Override
public PatientData<Medication> load(Patient patient) {
    try {/*from   www. j a v  a2s  .com*/
        XWikiDocument doc = (XWikiDocument) this.documentAccessBridge.getDocument(patient.getDocument());
        List<BaseObject> data = doc.getXObjects(Medication.CLASS_REFERENCE);
        if (data == null || data.isEmpty()) {
            this.logger.debug("No medication data for patient [{}]", patient.getDocument());
            return null;
        }
        List<Medication> result = new LinkedList<>();
        for (BaseObject medication : data) {
            if (medication == null) {
                continue;
            }
            MutablePeriod p = new MutablePeriod();
            p.setYears(medication.getIntValue(DURATION_YEARS));
            p.setMonths(medication.getIntValue(DURATION_MONTHS));
            result.add(new Medication(medication.getStringValue(Medication.NAME),
                    medication.getStringValue(Medication.GENERIC_NAME),
                    medication.getStringValue(Medication.DOSE), medication.getStringValue(Medication.FREQUENCY),
                    p.toPeriod(), medication.getStringValue(Medication.EFFECT),
                    medication.getLargeStringValue(Medication.NOTES)));
        }
        if (!result.isEmpty()) {
            return new IndexedPatientData<Medication>(DATA_NAME, result);
        }
    } catch (Exception ex) {
        this.logger.error("Could not find requested document or some unforeseen"
                + " error has occurred during controller loading ", ex.getMessage());
    }
    return null;
}

From source file:TVShowTimelineMaker.ui.Joda.PeriodEditor.java

/**
 * Creates new form PeriodEditor// w w w .  ja  va 2s  . co m
 */
public PeriodEditor() {
    this.initComponents();
    this.mMutablePeriod = new MutablePeriod();
    CollectionListModel<DurationFieldType> newCollectionListModel = new CollectionListModel<>();
    this.listFields.setModel(newCollectionListModel);
    newCollectionListModel.add(DurationFieldType.days());
    newCollectionListModel.add(DurationFieldType.hours());
    newCollectionListModel.add(DurationFieldType.millis());
    newCollectionListModel.add(DurationFieldType.minutes());
    newCollectionListModel.add(DurationFieldType.months());
    newCollectionListModel.add(DurationFieldType.seconds());
    newCollectionListModel.add(DurationFieldType.weeks());
    newCollectionListModel.add(DurationFieldType.years());
    this.txtOverview.setText(this.mMutablePeriod.toString());
    this.validate();
}