@Override public Policy removeEntry(final CharSequence label) { final Label lbl = Label.of(label); if (!entries.containsKey(lbl)) { return this; } final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.remove(lbl); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
/** * Returns a new immutable Policy based on the given JSON object. * * @param jsonObject the JSON object representation of a Policy. * @return the new initialised {@code Policy}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.model.base.exceptions.DittoJsonException if {@code jsonObject} cannot be parsed to * {@link Policy}. */ public static Policy newPolicy(final JsonObject jsonObject) { return ImmutablePolicy.fromJson(jsonObject); }
@Override public Iterator<PolicyEntry> iterator() { final Set<PolicyEntry> policyEntries = getEntriesSet(); return policyEntries.iterator(); }
@Override public Policy setSubjectFor(final CharSequence label, final Subject subject) { final Label lbl = Label.of(label); checkNotNull(subject, "subject to set to the Policy entry"); final Policy result; final PolicyEntry existingPolicyEntry = entries.get(lbl); if (null != existingPolicyEntry) { final Subjects existingSubjects = existingPolicyEntry.getSubjects(); final Subjects newSubjects = existingSubjects.setSubject(subject); if (!Objects.equals(existingSubjects, newSubjects)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, newSubjects, existingPolicyEntry.getResources())); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } else { result = this; } } else { result = setSubjectsFor(label, Subjects.newInstance(subject)); } return result; }
/** * Returns a new immutable Policy which is initialised with the specified entries. * * @param id the ID of the new Policy. * @param entries the entries of the Policy. * @return the new initialised Policy. * @throws NullPointerException if any argument is {@code null}. */ public static Policy newPolicy(final CharSequence id, final Iterable<PolicyEntry> entries) { return ImmutablePolicy.of(id, PolicyLifecycle.ACTIVE, PolicyRevision.newInstance(1), null, entries); }
@Override public Set<PolicyEntry> getEntriesSet() { return stream().collect(Collectors.toSet()); }
private ImmutablePolicy(@Nullable final CharSequence policyId, final Map<Label, PolicyEntry> theEntries, @Nullable final PolicyLifecycle lifecycle, @Nullable final PolicyRevision revision, @Nullable final Instant modified) { if (null != policyId) { PolicyIdValidator.getInstance().accept(policyId, DittoHeaders.empty()); this.policyId = policyId.toString(); } else { this.policyId = null; } entries = Collections.unmodifiableMap(new LinkedHashMap<>(theEntries)); namespace = parseNamespace(policyId); this.lifecycle = lifecycle; this.revision = revision; this.modified = modified; }
/** * Returns a new Policy which is initialised with the specified entries. * * @param policyId the ID of the new Policy. * @param lifecycle the lifecycle of the Policy to be created. * @param revision the revision of the Policy to be created. * @param modified the modified timestamp of the Policy to be created. * @param entries the entries of the Policy to be created. * @return a new initialised Policy. * @throws NullPointerException if {@code entries} is {@code null}. * @throws PolicyIdInvalidException if {@code policyId} did not comply to {@link Policy#ID_REGEX}. */ public static Policy of(@Nullable final CharSequence policyId, @Nullable final PolicyLifecycle lifecycle, @Nullable final PolicyRevision revision, @Nullable final Instant modified, final Iterable<PolicyEntry> entries) { checkNotNull(entries, "Policy entries"); final Map<Label, PolicyEntry> entryMap = new LinkedHashMap<>(); entries.forEach(policyEntry -> entryMap.put(policyEntry.getLabel(), policyEntry)); return new ImmutablePolicy(policyId, entryMap, lifecycle, revision, modified); }
@Override public Policy removeEntry(final PolicyEntry entry) { checkNotNull(entry, "Policy entry to be removed"); return removeEntry(entry.getLabel()); }
@Override public Policy build() { final Collection<Label> allLabels = getAllLabels(); final Collection<PolicyEntry> policyEntries = allLabels.stream() .map(lbl -> PoliciesModelFactory.newPolicyEntry(lbl, getFinalSubjects(lbl), getFinalResources(lbl))) .collect(Collectors.toList()); return ImmutablePolicy.of(id, lifecycle, revision, modified, policyEntries); }
@Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { checkNotNull(schemaVersion, "schema version"); checkNotNull(thePredicate, "predicate"); final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder(); jsonObjectBuilder.set(JsonFields.SCHEMA_VERSION, schemaVersion.toInt(), predicate); if (null != lifecycle) { jsonObjectBuilder.set(JsonFields.LIFECYCLE, lifecycle.name(), predicate); } if (null != revision) { jsonObjectBuilder.set(JsonFields.REVISION, revision.toLong(), predicate); } if (null != modified) { jsonObjectBuilder.set(JsonFields.MODIFIED, modified.toString(), predicate); } if (null != policyId) { jsonObjectBuilder.set(JsonFields.NAMESPACE, namespace, predicate); jsonObjectBuilder.set(JsonFields.ID, policyId, predicate); } jsonObjectBuilder.set(JsonFields.ENTRIES, stream() .map(policyEntry -> JsonFactory.newObjectBuilder() .set(policyEntry.getLabel().getJsonFieldDefinition(), policyEntry.toJson(schemaVersion, thePredicate.and(FieldType.notHidden())), predicate) // notice: only "not HIDDEN" sub-fields of PolicyEntry are included .build()) .collect(JsonCollectors.objectsToObject()), predicate); return jsonObjectBuilder.build(); }
@Override public Policy setEntry(final PolicyEntry policyEntry) { checkNotNull(policyEntry, "entry to be set to this Policy"); final Policy result; final PolicyEntry existingPolicyEntry = entries.get(policyEntry.getLabel()); if (null != existingPolicyEntry) { if (existingPolicyEntry.equals(policyEntry)) { result = this; } else { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(policyEntry.getLabel(), policyEntry); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } else { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(policyEntry.getLabel(), policyEntry); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } return result; }
/** * Returns a new immutable Policy which is initialised with the specified entries. * * @param id the ID of the new Policy. * @param entry the mandatory entry of the Policy. * @param furtherEntries additional entries of the Policy. * @return the new initialised Policy. * @throws NullPointerException if any argument is {@code null}. */ public static Policy newPolicy(final CharSequence id, final PolicyEntry entry, final PolicyEntry... furtherEntries) { checkNotNull(entry, "mandatory entry"); checkNotNull(furtherEntries, "additional policy entries"); final Collection<PolicyEntry> allEntries = new HashSet<>(1 + furtherEntries.length); allEntries.add(entry); Collections.addAll(allEntries, furtherEntries); return ImmutablePolicy.of(id, PolicyLifecycle.ACTIVE, PolicyRevision.newInstance(1), null, allEntries); }
@Override public Policy removeResourceFor(final CharSequence label, final ResourceKey resourceKey) { final Label lbl = Label.of(label); Policy result = this; final PolicyEntry existingEntry = entries.get(lbl); if (null != existingEntry) { final Resources existingResources = existingEntry.getResources(); final Resources newResources = existingResources.removeResource(resourceKey); if (!Objects.equals(existingResources, newResources)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, existingEntry.getSubjects(), newResources)); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } return result; }
.collect(Collectors.toSet()); return of(readId, readLifecycle, readRevision, readModified, policyEntries);
@Override public Policy removeSubjectFor(final CharSequence label, final SubjectId subjectId) { final Label lbl = Label.of(label); Policy result = this; final PolicyEntry existingPolicyEntry = entries.get(lbl); if (null != existingPolicyEntry) { final Subjects existingSubjects = existingPolicyEntry.getSubjects(); final Subjects newSubjects = existingSubjects.removeSubject(subjectId); if (!Objects.equals(existingSubjects, newSubjects)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, newSubjects, existingPolicyEntry.getResources())); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } return result; }
@Override public Policy setSubjectsFor(final CharSequence label, final Subjects subjects) { final Label lbl = Label.of(label); checkNotNull(subjects, "subjects to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry modifiedEntry; if (!entriesCopy.containsKey(lbl)) { modifiedEntry = newPolicyEntry(lbl, subjects, emptyResources()); } else { final PolicyEntry policyEntry = entriesCopy.get(lbl); modifiedEntry = newPolicyEntry(lbl, subjects, policyEntry.getResources()); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
@Override public Policy setResourcesFor(final CharSequence label, final Resources resources) { final Label lbl = Label.of(label); checkNotNull(resources, "resources to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry policyEntry = entriesCopy.get(lbl); final PolicyEntry modifiedEntry; if (null == policyEntry) { modifiedEntry = newPolicyEntry(lbl, PoliciesModelFactory.emptySubjects(), resources); } else { modifiedEntry = newPolicyEntry(lbl, policyEntry.getSubjects(), resources); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
@Override public Policy setResourceFor(final CharSequence label, final Resource resource) { final Label lbl = Label.of(label); checkNotNull(resource, "resource to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry modifiedEntry; if (!entriesCopy.containsKey(lbl)) { modifiedEntry = newPolicyEntry(label, PoliciesModelFactory.emptySubjects(), newResources(resource)); } else { final PolicyEntry policyEntry = entriesCopy.get(lbl); final Resources modifiedResources = policyEntry.getResources().setResource(resource); modifiedEntry = newPolicyEntry(label, policyEntry.getSubjects(), modifiedResources); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }