All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.james.jmap.model.Emailer Maven / Gradle / Ivy

/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you 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 org.apache.james.jmap.model;

import java.util.Objects;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;

@JsonDeserialize(builder = Emailer.Builder.class)
public class Emailer {

    public static Builder builder() {
        return new Builder();
    }

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
        private static final boolean DEFAULT_DISABLE = false;

        private Optional allowInvalid = Optional.empty();
        private Optional name = Optional.empty();
        private Optional email = Optional.empty();

        @JsonProperty("name")
        public Builder name(Optional name) {
            this.name = name;
            return this;
        }

        @JsonIgnore
        public Builder name(String name) {
            this.name = Optional.ofNullable(name);
            return this;
        }

        @JsonProperty("email")
        public Builder email(Optional email) {
            this.email = email;
            return this;
        }

        @JsonIgnore
        public Builder email(String email) {
            this.email = Optional.ofNullable(email);
            return this;
        }

        @JsonIgnore
        public Builder allowInvalid() {
            this.allowInvalid = Optional.of(true);
            return this;
        }

        public Emailer build() {
            if (allowInvalid.orElse(DEFAULT_DISABLE)) {
                return buildRelaxed();
            } else {
                return buildStrict();
            }
        }

        private Emailer buildStrict() {
            Preconditions.checkState(name.isPresent(), "'name' is mandatory");
            Preconditions.checkState(email.isPresent(), "'email' is mandatory");
            Preconditions.checkState(!name.get().isEmpty(), "'name' should not be empty");
            Preconditions.checkState(!email.get().isEmpty(), "'email' should not be empty");
            Preconditions.checkState(email.get().contains("@"), "'email' must contain '@' character");
            return new Emailer(name, email);
        }

        private Emailer buildRelaxed() {
            return new Emailer(replaceIfNeeded(name), replaceIfNeeded(email));
        }

        private Optional replaceIfNeeded(Optional value) {
            return value.filter(s -> !s.isEmpty());
        }
    }

    private final Optional name;
    private final Optional email;

    @VisibleForTesting Emailer(Optional name, Optional email) {
        this.name = name;
        this.email = email;
    }

    public Optional getName() {
        return name;
    }

    public Optional getEmail() {
        return email;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Emailer) {
            Emailer otherEMailer = (Emailer) o;
            return Objects.equals(name, otherEMailer.name)
                && Objects.equals(email, otherEMailer.email);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, email);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
            .add("name", name)
            .add("email", email)
            .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy