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

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.commons.dto.IdentityDTO.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.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.UUID;

/**
 * @author klenkes <rlichti@kaiserpfalz-edv.de>
 * @since 2014Q3
 */
public class IdentityDTO implements IdentityWriteable<UUID>, Comparable<Identity<UUID>> {
    private UUID id;

    public IdentityDTO() {
        id = UUID.randomUUID();
    }

    public IdentityDTO(final UUID id) {
        this.id = id;
    }

    public IdentityDTO(final String id) {
        this.id = UUID.fromString(id);
    }

    public IdentityDTO(final Identity<UUID> id) {
        this.id = id.getId();
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public void setId(UUID uuid) {
        id = uuid;
    }

    public void setId(Identity<UUID> id) {
        this.id = id.getId();
    }

    public IdentityDTO withId(final UUID id) {
        setId(id);
        return this;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).build();
    }

    @Override
    public boolean equals(final Object other) {
        if (other == this)
            return true;
        if (other == null)
            return false;
        if (!Identity.class.isAssignableFrom(other.getClass()))
            return false;

        @SuppressWarnings("unchecked")
        Identity<UUID> that = (Identity<UUID>) other;

        return new EqualsBuilder().append(getId(), that.getId()).build();
    }

    @Override
    public int compareTo(@SuppressWarnings("NullableProblems") final Identity<UUID> that) {
        return new CompareToBuilder().append(id, that.getId()).build();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(id).build();
    }
}