Java tutorial
/* * Copyright (c) 2014 Kaiserpfalz EDV-Service, Roland Lichti * * Licensed 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 de.kaiserpfalzEdv.iam.core.identity; import de.kaiserpfalzEdv.commons.NameableReadable; import de.kaiserpfalzEdv.iam.core.base.IAMBaseObjectBuilder; import de.kaiserpfalzEdv.iam.identity.Identity; import de.kaiserpfalzEdv.iam.identity.IdentityWritable; import de.kaiserpfalzEdv.iam.identity.Subject; import org.apache.commons.lang3.builder.Builder; import java.util.Collection; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentSkipListSet; /** * @author klenkes * @since 2013Q */ public class IdentityBuilder extends IAMBaseObjectBuilder<IdentityBuilder> implements Builder<IdentityWritable>, IAMBaseObjectBuilder.ConcreteObjectGenerator<IdentityWritable> { private final Set<SubjectDO> subjects = new ConcurrentSkipListSet<>(); public IdentityBuilder() { withGenerator(this); } @Override public IdentityDO build() { IdentityDO result; try { result = generate(); } catch (IllegalArgumentException e) { throw new IllegalStateException(e.getMessage(), e); } result.setSubjects(subjects); result.validate(); return result; } public IdentityBuilder withIdentity(final Identity identity) { withTenant(identity.getTenantId()); withId(identity.getId()); withName(identity.getName()); withCode(identity.getShortName()); withCanonicalName(identity.getCanonicalName()); withDisplayName(identity.getDisplayName()); withSubjects(identity.getSubjects()); return this; } public IdentityBuilder withSubjects(final Collection<? extends Subject> subjects) { this.subjects.clear(); if (subjects != null) { for (Subject r : subjects) { this.subjects.add(toSubjectDO(r)); } } return this; } public IdentityBuilder addSubject(final Subject subject) { if (subject != null) { this.subjects.add(toSubjectDO(subject)); } return this; } public IdentityBuilder removeSubject(final Subject subject) { if (subject != null) { this.subjects.remove(toSubjectDO(subject)); } return this; } private SubjectDO toSubjectDO(Subject subject) { if (subject == null) return null; if (SubjectDO.class.isAssignableFrom(subject.getClass())) { return (SubjectDO) subject; } return (SubjectDO) new SubjectBuilder().withSubject(subject).build(); } @Override public IdentityDO generate(final UUID tenant, final UUID id, final NameableReadable name) { return new IdentityDO(tenant, id, name); } }