85 lines
3.0 KiB
Java
Raw Normal View History

/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
2016-02-01 18:45:54 +00:00
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2016-02-01 18:45:54 +00:00
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2016-02-01 18:45:54 +00:00
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.featuretoggle.user;
import java.util.ArrayList;
import java.util.List;
2016-01-26 19:20:28 +00:00
/**
2016-01-28 21:14:40 +00:00
* Contains the lists of users of different groups paid and free. Used to demonstrate the tiered example of feature
* toggle. Allowing certain features to be available to only certain groups of users.
*
* @see User
2016-01-26 19:20:28 +00:00
*/
public class UserGroup {
2016-01-26 19:20:28 +00:00
private static List<User> freeGroup = new ArrayList<>();
private static List<User> paidGroup = new ArrayList<>();
2016-01-26 23:38:28 +00:00
2016-01-26 19:20:28 +00:00
/**
2016-01-28 21:14:40 +00:00
* Add the passed {@link User} to the free user group list.
2016-01-26 19:20:28 +00:00
*
* @param user {@link User} to be added to the free group
2016-01-31 13:46:34 +00:00
* @throws IllegalArgumentException when user is already added to the paid group
2016-01-28 21:14:40 +00:00
* @see User
2016-01-26 19:20:28 +00:00
*/
2016-01-28 21:14:40 +00:00
public static void addUserToFreeGroup(final User user) throws IllegalArgumentException {
2016-01-26 19:20:28 +00:00
if (paidGroup.contains(user)) {
throw new IllegalArgumentException("User all ready member of paid group.");
} else {
if (!freeGroup.contains(user)) {
freeGroup.add(user);
}
}
2016-01-26 19:20:28 +00:00
}
2016-01-26 19:20:28 +00:00
/**
2016-01-28 21:14:40 +00:00
* Add the passed {@link User} to the paid user group list.
2016-01-26 19:20:28 +00:00
*
* @param user {@link User} to be added to the paid group
2016-01-31 13:46:34 +00:00
* @throws IllegalArgumentException when the user is already added to the free group
2016-01-28 21:14:40 +00:00
* @see User
2016-01-26 19:20:28 +00:00
*/
2016-01-28 21:14:40 +00:00
public static void addUserToPaidGroup(final User user) throws IllegalArgumentException {
2016-01-26 19:20:28 +00:00
if (freeGroup.contains(user)) {
throw new IllegalArgumentException("User all ready member of free group.");
} else {
if (!paidGroup.contains(user)) {
paidGroup.add(user);
}
}
2016-01-26 19:20:28 +00:00
}
2016-01-31 13:46:34 +00:00
/**
* Method to take a {@link User} to determine if the user is in the {@link UserGroup#paidGroup}.
*
* @param user {@link User} to check if they are in the {@link UserGroup#paidGroup}
*
* @return true if the {@link User} is in {@link UserGroup#paidGroup}
*/
2016-01-26 19:20:28 +00:00
public static boolean isPaid(User user) {
return paidGroup.contains(user);
}
}