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

org.isda.cdm.Client Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package org.isda.cdm;

import com.google.common.collect.ImmutableList;
import com.rosetta.model.lib.RosettaModelObject;
import com.rosetta.model.lib.RosettaModelObjectBuilder;
import com.rosetta.model.lib.annotations.RosettaAttribute;
import com.rosetta.model.lib.annotations.RosettaDataType;
import com.rosetta.model.lib.meta.RosettaMetaData;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.process.BuilderMerger;
import com.rosetta.model.lib.process.BuilderProcessor;
import com.rosetta.model.lib.process.Processor;
import com.rosetta.util.ListEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.isda.cdm.Client;
import org.isda.cdm.Client.ClientBuilder;
import org.isda.cdm.Client.ClientBuilderImpl;
import org.isda.cdm.Client.ClientImpl;
import org.isda.cdm.Name;
import org.isda.cdm.meta.ClientMeta;

import static java.util.Optional.ofNullable;

/**
 * @version test
 */
@RosettaDataType(value="Client", builder=Client.ClientBuilderImpl.class, version="test")
public interface Client extends RosettaModelObject {

	ClientMeta metaData = new ClientMeta();

	/*********************** Getter Methods  ***********************/
	String getId();
	List getFirstName();
	Name getLastname();

	/*********************** Build Methods  ***********************/
	Client build();
	
	Client.ClientBuilder toBuilder();
	
	static Client.ClientBuilder builder() {
		return new Client.ClientBuilderImpl();
	}

	/*********************** Utility Methods  ***********************/
	@Override
	default RosettaMetaData metaData() {
		return metaData;
	}
	
	@Override
	default Class getType() {
		return Client.class;
	}
	
	
	@Override
	default void process(RosettaPath path, Processor processor) {
		processor.processBasic(path.newSubPath("id"), String.class, getId(), this);
		processor.processBasic(path.newSubPath("firstName"), String.class, getFirstName(), this);
		processRosetta(path.newSubPath("lastname"), processor, Name.class, getLastname());
	}
	

	/*********************** Builder Interface  ***********************/
	interface ClientBuilder extends Client, RosettaModelObjectBuilder {
		Name.NameBuilder getOrCreateLastname();
		Name.NameBuilder getLastname();
		Client.ClientBuilder setId(String id);
		Client.ClientBuilder addFirstName(String firstName0);
		Client.ClientBuilder addFirstName(String firstName1, int _idx);
		Client.ClientBuilder addFirstName(List firstName2);
		Client.ClientBuilder setFirstName(List firstName3);
		Client.ClientBuilder setLastname(Name lastname);

		@Override
		default void process(RosettaPath path, BuilderProcessor processor) {
			processor.processBasic(path.newSubPath("id"), String.class, getId(), this);
			processor.processBasic(path.newSubPath("firstName"), String.class, getFirstName(), this);
			processRosetta(path.newSubPath("lastname"), processor, Name.NameBuilder.class, getLastname());
		}
		

		Client.ClientBuilder prune();
	}

	/*********************** Immutable Implementation of Client  ***********************/
	class ClientImpl implements Client {
		private final String id;
		private final List firstName;
		private final Name lastname;
		
		protected ClientImpl(Client.ClientBuilder builder) {
			this.id = builder.getId();
			this.firstName = ofNullable(builder.getFirstName()).filter(_l->!_l.isEmpty()).map(ImmutableList::copyOf).orElse(null);
			this.lastname = ofNullable(builder.getLastname()).map(f->f.build()).orElse(null);
		}
		
		@Override
		@RosettaAttribute("id")
		public String getId() {
			return id;
		}
		
		@Override
		@RosettaAttribute("firstName")
		public List getFirstName() {
			return firstName;
		}
		
		@Override
		@RosettaAttribute("lastname")
		public Name getLastname() {
			return lastname;
		}
		
		@Override
		public Client build() {
			return this;
		}
		
		@Override
		public Client.ClientBuilder toBuilder() {
			Client.ClientBuilder builder = builder();
			setBuilderFields(builder);
			return builder;
		}
		
		protected void setBuilderFields(Client.ClientBuilder builder) {
			ofNullable(getId()).ifPresent(builder::setId);
			ofNullable(getFirstName()).ifPresent(builder::setFirstName);
			ofNullable(getLastname()).ifPresent(builder::setLastname);
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || !(o instanceof RosettaModelObject) || !getType().equals(((RosettaModelObject)o).getType())) return false;
		
			Client _that = getType().cast(o);
		
			if (!Objects.equals(id, _that.getId())) return false;
			if (!ListEquals.listEquals(firstName, _that.getFirstName())) return false;
			if (!Objects.equals(lastname, _that.getLastname())) return false;
			return true;
		}
		
		@Override
		public int hashCode() {
			int _result = 0;
			_result = 31 * _result + (id != null ? id.hashCode() : 0);
			_result = 31 * _result + (firstName != null ? firstName.hashCode() : 0);
			_result = 31 * _result + (lastname != null ? lastname.hashCode() : 0);
			return _result;
		}
		
		@Override
		public String toString() {
			return "Client {" +
				"id=" + this.id + ", " +
				"firstName=" + this.firstName + ", " +
				"lastname=" + this.lastname +
			'}';
		}
	}

	/*********************** Builder Implementation of Client  ***********************/
	class ClientBuilderImpl implements Client.ClientBuilder {
	
		protected String id;
		protected List firstName = new ArrayList<>();
		protected Name.NameBuilder lastname;
	
		public ClientBuilderImpl() {
		}
	
		@Override
		@RosettaAttribute("id")
		public String getId() {
			return id;
		}
		
		@Override
		@RosettaAttribute("firstName")
		public List getFirstName() {
			return firstName;
		}
		
		@Override
		@RosettaAttribute("lastname")
		public Name.NameBuilder getLastname() {
			return lastname;
		}
		
		@Override
		public Name.NameBuilder getOrCreateLastname() {
			Name.NameBuilder result;
			if (lastname!=null) {
				result = lastname;
			}
			else {
				result = lastname = Name.builder();
			}
			
			return result;
		}
		
		@Override
		@RosettaAttribute("id")
		public Client.ClientBuilder setId(String id) {
			this.id = id==null?null:id;
			return this;
		}
		@Override
		public Client.ClientBuilder addFirstName(String firstName) {
			if (firstName!=null) this.firstName.add(firstName);
			return this;
		}
		
		@Override
		public Client.ClientBuilder addFirstName(String firstName, int _idx) {
			getIndex(this.firstName, _idx, () -> firstName);
			return this;
		}
		@Override 
		public Client.ClientBuilder addFirstName(List firstNames) {
			if (firstNames != null) {
				for (String toAdd : firstNames) {
					this.firstName.add(toAdd);
				}
			}
			return this;
		}
		
		@Override 
		@RosettaAttribute("firstName")
		public Client.ClientBuilder setFirstName(List firstNames) {
			if (firstNames == null)  {
				this.firstName = new ArrayList<>();
			}
			else {
				this.firstName = firstNames.stream()
					.collect(Collectors.toCollection(()->new ArrayList<>()));
			}
			return this;
		}
		
		@Override
		@RosettaAttribute("lastname")
		public Client.ClientBuilder setLastname(Name lastname) {
			this.lastname = lastname==null?null:lastname.toBuilder();
			return this;
		}
		
		@Override
		public Client build() {
			return new Client.ClientImpl(this);
		}
		
		@Override
		public Client.ClientBuilder toBuilder() {
			return this;
		}
	
		@SuppressWarnings("unchecked")
		@Override
		public Client.ClientBuilder prune() {
			if (lastname!=null && !lastname.prune().hasData()) lastname = null;
			return this;
		}
		
		@Override
		public boolean hasData() {
			if (getId()!=null) return true;
			if (getFirstName()!=null && !getFirstName().isEmpty()) return true;
			if (getLastname()!=null && getLastname().hasData()) return true;
			return false;
		}
	
		@SuppressWarnings("unchecked")
		@Override
		public Client.ClientBuilder merge(RosettaModelObjectBuilder other, BuilderMerger merger) {
			Client.ClientBuilder o = (Client.ClientBuilder) other;
			
			merger.mergeRosetta(getLastname(), o.getLastname(), this::setLastname);
			
			merger.mergeBasic(getId(), o.getId(), this::setId);
			merger.mergeBasic(getFirstName(), o.getFirstName(), (Consumer) this::addFirstName);
			return this;
		}
	
		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || !(o instanceof RosettaModelObject) || !getType().equals(((RosettaModelObject)o).getType())) return false;
		
			Client _that = getType().cast(o);
		
			if (!Objects.equals(id, _that.getId())) return false;
			if (!ListEquals.listEquals(firstName, _that.getFirstName())) return false;
			if (!Objects.equals(lastname, _that.getLastname())) return false;
			return true;
		}
		
		@Override
		public int hashCode() {
			int _result = 0;
			_result = 31 * _result + (id != null ? id.hashCode() : 0);
			_result = 31 * _result + (firstName != null ? firstName.hashCode() : 0);
			_result = 31 * _result + (lastname != null ? lastname.hashCode() : 0);
			return _result;
		}
		
		@Override
		public String toString() {
			return "ClientBuilder {" +
				"id=" + this.id + ", " +
				"firstName=" + this.firstName + ", " +
				"lastname=" + this.lastname +
			'}';
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy