/** * Performs an action on each Subject in the collection. * * <p>Subjects are loaded, supplied to the consumer, and then allowed to be * uncached by the implementation.</p> * * <p>This should be used to apply bulk changes or gather data about all * Subjects in the collection. The provided consumer will be supplied * asynchronously. Acting upon a large collection may be particularly * resource intensive.</p> * * <p>Implementations may choose to load and process subjects in * parallel.</p> * * @param action The action to perform on each subject * @return A future which will complete when the operation has finished */ default CompletableFuture<Void> applyToAll(Consumer<Subject> action) { Preconditions.checkNotNull(action, "action"); return CompletableFuture.runAsync(() -> { Set<String> identifiers = getAllIdentifiers().join(); applyToAll(action, identifiers).join(); }); }
/** * Performs an action on each Subject in the provided set. * * <p>Subjects are loaded, supplied to the consumer, and then allowed to be * uncached by the implementation.</p> * * <p>This should be used to apply bulk changes or gather data about all * Subjects in the collection. The provided consumer will be supplied * asynchronously. Acting upon a large collection may be particularly * resource intensive.</p> * * <p>Implementations may choose to load and process subjects in * parallel.</p> * * @param action The action to perform on each subject * @param identifiers a set of identifiers to apply the action to * @return A future which will complete when the operation has finished */ default CompletableFuture<Void> applyToAll(Consumer<Subject> action, Set<String> identifiers) { Preconditions.checkNotNull(action, "action"); Preconditions.checkNotNull(identifiers, "identifiers"); return CompletableFuture.runAsync(() -> { for (String id : identifiers) { Subject subject = loadSubject(id).join(); action.accept(subject); suggestUnload(subject.getIdentifier()); } }); }
@ApiModelProperty(value = "The unique id of this subject collection", required = true) public String getId() { return value.getIdentifier(); }
@GET @Path("/collection/{id}/subject") @Permission({ "collection", "subject", "list" }) @ExplicitDetails @ApiOperation( value = "List subjects", notes = "List all subjects belonging to a certain collection") public Set<Subject> listSubjects(@PathParam("id") String id) throws NotFoundException { PermissionService srv = getPermissionService(); try { if (!srv.hasCollection(id).get()) throw new NotFoundException("Collection with id " + id + " could not be found"); Set<Subject> subjects = new HashSet<>(); SubjectCollection coll = srv.loadCollection(id).get(); for (String subId : coll.getAllIdentifiers().get()) { subjects.add(coll.loadSubject(subId).get()); } return subjects; } catch (InterruptedException e) { throw new ClientErrorException(e.getMessage(), Response.Status.REQUEST_TIMEOUT); } catch (ExecutionException e) { throw new InternalServerErrorException(e.getMessage()); } }
@GET @Path("/collection/{id}/subject/{subId}") @Permission({ "collection", "subject", "one" }) @ApiOperation( value = "Get subject", notes = "Gets one specific subject belonging to a certain collection") public Subject getSubject(@PathParam("id") String id, @PathParam("subId") String subId) throws NotFoundException { PermissionService srv = getPermissionService(); try { if (!srv.hasCollection(id).get()) throw new NotFoundException("Collection with id " + id + " could not be found"); SubjectCollection coll = srv.loadCollection(id).get(); if (!coll.hasSubject(subId).get()) throw new NotFoundException("Subject with id " + id + " could not be found"); return coll.loadSubject(subId).get(); } catch (InterruptedException e) { throw new ClientErrorException(e.getMessage(), Response.Status.REQUEST_TIMEOUT); } catch (ExecutionException e) { throw new InternalServerErrorException(e.getMessage()); } }
List<Subject> toSubjectList(List<Map.Entry<String, String>> parents) { ImmutableList.Builder<Subject> ret = ImmutableList.builder(); for (Map.Entry<String, String> ent : parents) { SubjectCollection collection = this.service.getSubjects(ent.getKey()); ret.add(collection.get(ent.getValue())); } return ret.build(); }
@ApiModelProperty(value = "The amount of subjects currently loaded from this collection", required = true) public Integer getLoadedSubjectCount() { return value.getLoadedSubjects().size(); }
@Override public Collection<MessageReceiver> getMembers() { PermissionService service = Sponge.getGame().getServiceManager().provideUnchecked(PermissionService.class); return service.getKnownSubjects().values().stream() .flatMap(input -> input.getAllWithPermission(this.permission).entrySet().stream() .filter(Map.Entry::getValue) .map(entry -> entry.getKey().getCommandSource().orElse(null)) .filter(source -> source != null)) .collect(GuavaCollectors.toImmutableSet()); }
@Override public Optional<Map.Entry<String, String>> getSubjectIdentifier() { return Optional.of(Maps.immutableEntry(commandSource.getContainingCollection().getIdentifier(), commandSource.getIdentifier())); }
@Override public boolean checkPermission(String playerName, String perm) { if (playerName.equals("console")) { return true; } UUID uuid = getUUID(playerName); return loader.getGame().getServiceManager().provide(PermissionService.class).get().getUserSubjects().get(uuid.toString()).hasPermission(perm); }
@Override public boolean addParent(Set<Context> contexts, Subject parent) { contexts = ImmutableSet.copyOf(contexts); while (true) { Map.Entry<String, String> newEnt = Maps.immutableEntry(parent.getContainingCollection().getIdentifier(), parent.getIdentifier()); List<Map.Entry<String, String>> oldParents = this.parents.get(contexts); List<Map.Entry<String, String>> newParents = ImmutableList.<Map.Entry<String, String>>builder() .addAll(oldParents == null ? Collections.emptyList() : oldParents) .add(newEnt) .build(); if (oldParents != null && oldParents.contains(newEnt)) { return false; } if (updateCollection(this.parents, contexts, oldParents, newParents)) { return true; } } }
@Override public boolean removeParent(Set<Context> contexts, Subject parent) { contexts = ImmutableSet.copyOf(contexts); while (true) { Map.Entry<String, String> removeEnt = Maps.immutableEntry(parent.getContainingCollection().getIdentifier(), parent.getIdentifier()); List<Map.Entry<String, String>> oldParents = this.parents.get(contexts); List<Map.Entry<String, String>> newParents; if (oldParents == null || !oldParents.contains(removeEnt)) { return false; } newParents = new ArrayList<>(oldParents); newParents.remove(removeEnt); if (updateCollection(this.parents, contexts, oldParents, Collections.unmodifiableList(newParents))) { return true; } } }
public static String getHighestGroup(Player player) { try { if (!Sponge.getGame().getServiceManager().getRegistration(PermissionService.class).isPresent()) return ""; PermissionService ps = Sponge.getGame().getServiceManager().getRegistration(PermissionService.class).get().getProvider(); HashMap<Integer, Subject> subs = new HashMap<Integer, Subject>(); for (SubjectReference sub : player.getParents()) { if (sub.getCollectionIdentifier().equals(ps.getGroupSubjects().getIdentifier()) && (sub.getSubjectIdentifier() != null)) { Subject subj = sub.resolve().get(); subs.put(subj.getParents().size(), subj); } } return subs.isEmpty() ? "" : subs.get(Collections.max(subs.keySet())).getFriendlyIdentifier().isPresent() ? subs.get(Collections.max(subs.keySet())).getFriendlyIdentifier().get() : ""; } catch (InterruptedException | ExecutionException e) { } return ""; }
@Override public boolean addParent(final Set<Context> set, final Subject subject) { return wasSuccess(data.update(input -> input.addParent(parSet(set), subject.getContainingCollection().getIdentifier(), subject.getIdentifier()))); }
@Override public boolean removeParent(final Set<Context> set, final Subject subject) { return wasSuccess(data.update(input -> input.removeParent(parSet(set), subject.getContainingCollection().getIdentifier(), subject.getIdentifier()))); }