de.softmetz.commons.ddd.shared.vo.AbstractStringValueObject.java Source code

Java tutorial

Introduction

Here is the source code for de.softmetz.commons.ddd.shared.vo.AbstractStringValueObject.java

Source

/*
 *  commons-ddd is a set of Java classes for use in Domain Driven Design.
 *  Copyright (C) 2013  Christian Kalkhoff <softmetz@fsfe.org>
 *
 *  This file is part of commons-ddd.
 *
 *  commons-ddd is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  commons-ddd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with commons-ddd.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.softmetz.commons.ddd.shared.vo;

import javax.persistence.MappedSuperclass;
import org.apache.commons.lang3.Validate;

/**
 * Abstract base class for value objects that consist of exactly one String attribute.
 *
 * This class implements {@link CharSequence} and delegates all of its methods to the encapsulated String value.
 *
 * @author softmetz
 * @param <VO> Type of the concrete implementation class
 */
@MappedSuperclass
public abstract class AbstractStringValueObject<VO extends AbstractStringValueObject<VO>>
        extends AbstractComparableSingleValueObject<String, VO> implements CharSequence {

    protected AbstractStringValueObject() {
        // Provided for proxying
    }

    protected AbstractStringValueObject(String value) {
        super(value);
        Validate.notEmpty(value);
    }

    @Override
    public String toString() {
        return getValue();
    }

    @Override
    public int length() {
        return getValue().length();
    }

    @Override
    public char charAt(int index) {
        return getValue().charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return getValue().subSequence(start, end);
    }
}