ImmutablePolicy implements Policy { @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, created); } } return result; } private  ImmutablePolicy(@Nullable final PolicyId policyId,
            final Map<Label, PolicyEntry> theEntries,
            @Nullable final PolicyLifecycle lifecycle,
            @Nullable final PolicyRevision revision,
            @Nullable final Instant modified,
            @Nullable final Instant created); @Deprecated static Policy of(@Nullable final String policyId,
            @Nullable final PolicyLifecycle lifecycle,
            @Nullable final PolicyRevision revision,
            @Nullable final Instant modified,
            @Nullable final Instant created,
            final Iterable<PolicyEntry> entries); static Policy of(@Nullable final PolicyId policyId,
            @Nullable final PolicyLifecycle lifecycle,
            @Nullable final PolicyRevision revision,
            @Nullable final Instant modified,
            @Nullable final Instant created,
            final Iterable<PolicyEntry> entries); static Policy fromJson(final JsonObject jsonObject); @Override Optional<PolicyId> getEntityId(); @Override Optional<String> getNamespace(); @Override Optional<PolicyLifecycle> getLifecycle(); @Override Optional<PolicyRevision> getRevision(); @Override Optional<Instant> getModified(); @Override Optional<Instant> getCreated(); @Override Optional<Metadata> getMetadata(); @Override boolean isDeleted(); @Override Policy setEntry(final PolicyEntry policyEntry); @Override Set<Label> getLabels(); @Override boolean contains(final CharSequence label); @Override Optional<PolicyEntry> getEntryFor(final CharSequence label); @Override Policy removeEntry(final CharSequence label); @Override Policy removeEntry(final PolicyEntry entry); @Override Policy setSubjectsFor(final CharSequence label, final Subjects subjects); @Override Policy setSubjectFor(final CharSequence label, final Subject subject); @Override Policy removeSubjectFor(final CharSequence label, final SubjectId subjectId); @Override Policy setResourcesFor(final CharSequence label, final Resources resources); @Override Policy setResourceFor(final CharSequence label, final Resource resource); @Override Policy removeResourceFor(final CharSequence label, final ResourceKey resourceKey); @Override Optional<EffectedPermissions> getEffectedPermissionsFor(final CharSequence label, final SubjectId subjectId,
            final ResourceKey resourceKey); @Override boolean isEmpty(); @Override int getSize(); @Override Set<PolicyEntry> getEntriesSet(); @Override Stream<PolicyEntry> stream(); @Override Iterator<PolicyEntry> iterator(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void removeResourceForShouldCopyExisting() { final Policy policy = createPolicy(); final Policy policyModified = policy.removeResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, END_USER_RESOURCE_1); assertThat(policy).hasLabel(END_USER_LABEL); assertThat(policy).hasSubjectFor(END_USER_LABEL, END_USER_SUBJECT_ID_1); assertThat(policy).hasSubjectTypeFor(END_USER_LABEL, END_USER_SUBJECT_ID_1, END_USER_SUBJECT_TYPE_1); assertThat(policy).hasResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, END_USER_RESOURCE_1); assertThat(policyModified).hasLabel(END_USER_LABEL); assertThat(policyModified).hasSubjectFor(END_USER_LABEL, END_USER_SUBJECT_ID_1); assertThat(policyModified).hasSubjectTypeFor(END_USER_LABEL, END_USER_SUBJECT_ID_1, END_USER_SUBJECT_TYPE_1); assertThat(policyModified).doesNotHaveResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, END_USER_RESOURCE_1); }