001package ball.persistence.embeddable;
002/*-
003 * ##########################################################################
004 * Persistence Implementation (Hibernate)
005 * $Id: PersonName.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-persistence/trunk/src/main/java/ball/persistence/embeddable/PersonName.java $
007 * %%
008 * Copyright (C) 2016 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.util.stream.Collectors;
024import java.util.stream.Stream;
025import javax.persistence.Column;
026import javax.persistence.Embeddable;
027import javax.persistence.Lob;
028import lombok.EqualsAndHashCode;
029import lombok.Getter;
030import lombok.NoArgsConstructor;
031import lombok.Setter;
032
033import static org.apache.commons.lang3.StringUtils.SPACE;
034import static org.apache.commons.lang3.StringUtils.isEmpty;
035
036/**
037 * {@link PersonName} {@link Embeddable}.
038 *
039 * {@bean.info}
040 *
041 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
042 * @version $Revision: 5285 $
043 */
044@Embeddable
045@NoArgsConstructor @Getter @Setter @EqualsAndHashCode(callSuper = false)
046public class PersonName {
047    @Column(length = 12)
048    private String title = null;
049
050    @Column(length = 32)
051    private String first = null;
052
053    @Column(length = 32)
054    private String middle = null;
055
056    @Column(length = 32)
057    private String last = null;
058
059    @Column(length = 12)
060    private String suffix = null;
061
062    @Column @Lob
063    private String alias = null;
064
065    /**
066     * Method to clear all fields.
067     */
068    public void clear() {
069        setTitle(null);
070        setFirst(null);
071        setMiddle(null);
072        setLast(null);
073        setSuffix(null);
074        setAlias(null);
075    }
076
077    @Override
078    public String toString() {
079        String string = getAlias();
080
081        if (isEmpty(string)) {
082            string =
083                Stream.of(title, first, middle, last, suffix)
084                .filter(s -> (! isEmpty(s)))
085                .collect(Collectors.joining(SPACE));
086        }
087
088        return string;
089    }
090}