de.kaiserpfalzEdv.commons.dto.NameBuilder.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.commons.dto.NameBuilder.java

Source

/*
 * Copyright 2015 Kaiserpfalz EDV-Service Roland Lichti
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.kaiserpfalzEdv.commons.dto;

import org.apache.commons.lang3.builder.Builder;
import org.apache.commons.lang3.builder.CompareToBuilder;

import java.util.Comparator;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * @author klenkes
 * @since 2014Q1
 */
public class NameBuilder implements Builder<NameableWritable> {
    private String displayName;
    private String shortName;
    private String canonicalName;

    public NameDTO build() {
        validate();

        NameDTO result = new NameDTO(displayName, shortName, canonicalName);

        return result;
    }

    /**
     * <p>Checks if a valid {@link NameDTO} object could be build.</p>
     * <p>No name part bay be empty, {@code null} or contains only of blanks. Additionally the maximum sizes
     * are checked (display name against {@value NameDTO#MAX_DISPLAY_NAME}
     * ({@link NameDTO#MAX_DISPLAY_NAME}), short name against
     * {@value NameDTO#MAX_SHORT_NAME}
     * ({@link NameDTO#MAX_SHORT_NAME}) and canonical name against
     * {@value NameDTO#MAX_CANONICAL_NAME}
     * ({@link NameDTO#MAX_CANONICAL_NAME})).</p>
     *
     * @throws java.lang.IllegalStateException If the current data state would not be sufficient to create a valid
     *                                         {@link NameDTO} object.
     */
    public void validate() {
        checkName(displayName, "display name", NameDTO.MAX_DISPLAY_NAME);
        checkName(canonicalName, "canonical name", NameDTO.MAX_CANONICAL_NAME);
        checkName(shortName, "short name", NameDTO.MAX_SHORT_NAME);
    }

    private void checkName(final String value, final String name, final int maxLength) {
        if (isBlank(value))
            throw new IllegalStateException("Can't create a Nameable without " + name + "!");

        if (value.length() > maxLength)
            throw new IllegalStateException(name + " is too long (" + maxLength + " chars max, " + value.length()
                    + " is " + (value.length() - maxLength) + " too long)!");
    }

    public NameBuilder withName(final Nameable data) {
        withDisplayName(data.getDisplayName());
        withShortName(data.getShortName());
        withCanonicalName(data.getCanonicalName());

        return this;
    }

    public NameBuilder withName(final String data) {
        withDisplayName(data);
        withShortName(data);
        withCanonicalName(data);

        return this;
    }

    public NameBuilder withDisplayName(final String data) {
        this.displayName = data;
        return this;
    }

    public NameBuilder withShortName(final String data) {
        this.shortName = data;
        return this;
    }

    public NameBuilder withCanonicalName(final String data) {
        this.canonicalName = data;
        return this;
    }

    public Comparator<Nameable> getDisplayNameComparator() {
        return new Comparator<Nameable>() {
            @Override
            public int compare(Nameable o1, Nameable o2) {
                return new CompareToBuilder().append(o1.getDisplayName(), o2.getDisplayName()).build();
            }

            @Override
            public String toString() {
                return "DisplayNameComparator@" + System.identityHashCode(this);
            }
        };
    }

    public Comparator<Nameable> getShortNameComparator() {
        return new Comparator<Nameable>() {
            @Override
            public int compare(Nameable o1, Nameable o2) {
                return new CompareToBuilder().append(o1.getShortName(), o2.getShortName()).build();
            }

            @Override
            public String toString() {
                return "ShortNameComparator@" + System.identityHashCode(this);
            }
        };
    }

    public Comparator<Nameable> getCanonicalNameComparator() {
        return new Comparator<Nameable>() {
            @Override
            public int compare(Nameable o1, Nameable o2) {
                return new CompareToBuilder().append(o1.getCanonicalName(), o2.getCanonicalName()).build();
            }

            @Override
            public String toString() {
                return "CanonicalNameComparator@" + System.identityHashCode(this);
            }
        };
    }
}