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.Subject; import de.kaiserpfalzEdv.iam.identity.SubjectWritable; import de.kaiserpfalzEdv.iam.role.Role; import org.apache.commons.lang3.builder.Builder; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; /** * @author klenkes * @since 2014Q */ public class SubjectBuilder extends IAMBaseObjectBuilder<SubjectBuilder> implements Builder<SubjectWritable>, IAMBaseObjectBuilder.ConcreteObjectGenerator<SubjectWritable> { private Identity identity; private List<Role> roles = new ArrayList<>(); public SubjectBuilder() { withGenerator(this); } @Override public SubjectDO build() { SubjectDO result; try { result = generate(); } catch (IllegalArgumentException e) { throw new IllegalStateException(e.getMessage(), e); } if (identity != null) result.setIdentity(identity); for (Role r : roles) result.addRole(r); result.validate(); return result; } @Override public SubjectWritable generate(UUID tenant, UUID id, NameableReadable name) { return new SubjectDO(tenant, id, name); } public SubjectBuilder withSubject(Subject subject) { withTenant(subject.getTenantId()); withId(subject.getId()); withName(subject.getNameable()); withIdentity(subject.getIdentity()); withRoles(subject.getRoles()); return this; } public SubjectBuilder withIdentity(final Identity identity) { this.identity = identity; return this; } public SubjectBuilder withRoles(Collection<? extends Role> roles) { this.roles.clear(); if (this.roles != null) { this.roles.addAll(roles); } return this; } public SubjectBuilder addRole(Role role) { if (role != null) roles.add(role); return this; } public SubjectBuilder removeRole(Role role) { if (role != null) roles.remove(role); return this; } }