Java tutorial
/* * Copyright 2015 the original author or authors. * * 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 io.curly.gathering.list; import io.curly.gathering.item.ListItem; import io.curly.gathering.mention.UserParticipant; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.jetbrains.annotations.NotNull; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import java.io.Serializable; import java.util.Collections; import java.util.HashSet; import java.util.Set; import static org.springframework.util.Assert.notNull; /** * @author Joo Pedro Evangelista */ @Data @Document @NoArgsConstructor @AllArgsConstructor public class GatheringList implements Serializable { @Id private String id; @Field private String owner; @Field private String name; @Field private Set<UserParticipant> participants; @Field private Set<ListItem> items; public GatheringList(String name, String owner) { this.id = null; this.owner = owner; this.name = name; this.participants = new HashSet<>(0); this.items = new HashSet<>(0); } public String getId() { return id; } @NotNull public String getOwner() { return owner; } @NotNull public Set<UserParticipant> getParticipants() { return Collections.unmodifiableSet(this.participants); } @NotNull public Set<ListItem> getItems() { return Collections.unmodifiableSet(this.items); } public String getName() { return this.name; } @NotNull public GatheringList add(@NotNull ListItem item) { notNull(item, "To add a item, it must be not null!"); this.items.add(item); return new GatheringList(this.id, this.owner, this.name, this.participants, this.items); } @NotNull public GatheringList add(@NotNull UserParticipant participant) { this.participants.add(participant); return new GatheringList(this.id, this.owner, this.name, this.participants, this.items); } @NotNull public GatheringList remove(@NotNull ListItem item) { this.items.remove(item); return new GatheringList(this.id, this.owner, this.name, this.participants, this.items); } @NotNull public GatheringList remove(@NotNull UserParticipant userParticipant) { this.participants.remove(userParticipant); return new GatheringList(this.id, this.owner, this.name, this.participants, this.items); } }