OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils.text;

import org.openconcerto.utils.CollectionUtils;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import net.jcip.annotations.Immutable;

@Immutable
public final class DateTimeFormat extends DateTimeComponents {

    public static final class Literal extends DateTimeFormatComponent {
        private final String value;

        Literal(String value) {
            super();
            this.value = Objects.requireNonNull(value);
        }

        public final String getValue() {
            return this.value;
        }

        @Override
        public int hashCode() {
            return Objects.hash(this.value);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Literal other = (Literal) obj;
            return this.value.equals(other.value);
        }

        @Override
        public String toString() {
            return this.getClass().getSimpleName() + ' ' + this.getValue();
        }
    }

    private final List<DateTimeFormatComponent> components;

    DateTimeFormat(List<? extends DateTimeFormatComponent> components) {
        super();
        this.components = CollectionUtils.toImmutableList(Objects.requireNonNull(components));
    }

    @Override
    public final List<DateTimeFormatComponent> getComponents() {
        return this.components;
    }

    public final DateTimeFormatterBuilder createJavaFormatterBuilder() {
        return DateProp.createJavaFormatterBuilder(this.getComponents());
    }

    public final DateTimeFormatter createJavaFormatter(final Locale locale) {
        return this.createJavaFormatterBuilder().toFormatter(locale);
    }
}