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

org.apache.james.jmap.model.SetError 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.Optional;
import java.util.Set;

import org.apache.james.jmap.model.MessageProperties.MessageProperty;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

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

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

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {

        private String type;
        private String description;
        private Optional> properties = Optional.empty();

        protected Builder() {
        }

        public Builder type(String type) {
            this.type = type;
            return this;
        }

        public Builder description(String description) {
            this.description = description;
            return this;
        }

        public Builder properties(MessageProperty... properties) {
            return properties(ImmutableSet.copyOf(properties));
        }

        public Builder properties(Set properties) {
            this.properties = Optional.of(Sets.union(
                    this.properties.orElse(ImmutableSet.of()),
                    Optional.ofNullable(properties).orElse(ImmutableSet.of()))
                    .immutableCopy());
            return this;
        }

        public SetError build() {
            Preconditions.checkState(!Strings.isNullOrEmpty(type), "'type' is mandatory");
            return new SetError(type, Optional.ofNullable(description), properties);
        }
    }

    private final String type;
    private final Optional description;
    private final Optional> properties;


    @VisibleForTesting SetError(String type, Optional description, Optional> properties) {
        this.type = type;
        this.description = description;
        this.properties = properties;
    }

    protected SetError(SetError setError) {
        this.type = setError.type;
        this.description = setError.description;
        this.properties = setError.properties;
    }

    
    @JsonSerialize
    public String getType() {
        return type;
    }

    @JsonSerialize
    public Optional getDescription() {
        return description;
    }

    @JsonSerialize
    public Optional> getProperties() {
        return properties;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof SetError) {
            SetError other = (SetError) obj;
            return Objects.equal(this.type, other.type)
                && Objects.equal(this.description, other.description)
                && Objects.equal(this.properties, other.properties);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(type, description, properties);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("description", description)
                .add("type", type)
                .add("properties", properties)
                .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy