Last active
January 30, 2017 03:56
-
-
Save jamesladd/087c6e6abe03e64d5b51c2c9b97cb2f1 to your computer and use it in GitHub Desktop.
An East Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.east; | |
import java.util.*; | |
interface ContainedListener { | |
void notifyContained(Object item, boolean matched); | |
} | |
public class PermissionService implements ContainedListener { | |
public static void main(String[] args) { | |
new PermissionService().run(); | |
} | |
private void run() { | |
Permissions setA = new Permissions("A", "E", "I", "O", "U"); | |
Permissions setB = new Permissions("E", "O"); | |
setA.contains(setB, this); | |
} | |
@Override public void notifyContained(Object item, boolean contained) { | |
if (contained) | |
System.out.println("Contains: " + item); | |
} | |
private static class Permissions { | |
private final List<Permission> contents = new ArrayList<>(); | |
public Permissions(String ... arguments) { | |
for (String entry : arguments) | |
add(createEntry(entry)); | |
} | |
public void contains(Permissions toMatch, ContainedListener containedListener) { | |
for (Permission permission : contents) | |
toMatch.contains(permission, containedListener); | |
} | |
private void contains(Permission permission, ContainedListener containedListener) { | |
containedListener.notifyContained(permission, contents.contains(permission)); | |
} | |
private void add(Permission permission) { | |
contents.add(permission); | |
} | |
private Permission createEntry(String value) { | |
return new Permission(value); | |
} | |
} | |
private static class Permission { | |
private final String value; | |
Permission(String value) { | |
this.value = value; | |
} | |
public String toString() { | |
return value; | |
} | |
public boolean equals(Object other) { | |
return other instanceof Permission | |
&& value.equals(((Permission) other).value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment