/** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved * * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at * https://opensso.dev.java.net/public/CDDLv1.0.html or * opensso/legal/CDDLv1.0.txt * See the License for the specific language governing * permission and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at opensso/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * $Id: ConditionDecision.java,v 1.3 2009/09/05 00:24:04 veiming Exp $ * * Portions copyright 2010-2015 ForgeRock AS. */ package com.sun.identity.entitlement; import org.forgerock.util.Reject; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Class to represent {@link EntitlementCondition} evaluation match result and - if applicable - its advice. * * @supported.all.api */ public class ConditionDecision { private final Map> responseAttributes; private final Map> advice; private final long timeToLive; private final boolean satisfied; /** * Constructs an instance of ConditionDecision. *

* Deprecated, favour the factory methods. * * @param satisfied * result of this ConditionDecision. * @param advices * Advice map of this ConditionDecision. */ @Deprecated public ConditionDecision(boolean satisfied, Map> advices) { this(satisfied, advices, Long.MAX_VALUE); } /** * Constructs an instance of ConditionDecision. *

* Deprecated, favour the factory methods. * * @param satisfied * Result of this ConditionDecision. * @param advices * Advice map of this ConditionDecision. * @param ttl * The TTL of this ConditionDecision. */ @Deprecated public ConditionDecision(boolean satisfied, Map> advices, long ttl) { this.satisfied = satisfied; this.advice = new HashMap<>(advices); this.responseAttributes = new HashMap<>(); this.timeToLive = ttl; } private ConditionDecision(Builder builder) { satisfied = builder.satisfied; advice = builder.advice; responseAttributes = builder.responseAttributes; timeToLive = builder.timeToLive; } /** * Whether this ConditionDecision is satisfied. * * @return true if ConditionDecision is fulfilled. */ public boolean isSatisfied() { return satisfied; } /** * Advices associated with this ConditionDecision. * * @return advice of ConditionDecision. */ public Map> getAdvice() { return Collections.unmodifiableMap(advice); } /** * Retrieves the response attributes. * * @return the response attributes */ public Map> getResponseAttributes() { return Collections.unmodifiableMap(responseAttributes); } /** * Returns the time to live (TTL) of this ConditionDecision. * * @return The TTL time in ms. */ public long getTimeToLive() { return timeToLive; } /** * Clears the current advice associated with this ConditionDecision. *

* Deprecated method as a given instance should be immutable. */ @Deprecated public void clearAdvices() { advice.clear(); } /** * Adds an advice (from another ConditionDecision) to this ConditionDecision. *

* Deprecated method as a given instance should be immutable. * * @param decision * The ConditionDecision whose advice should be added to this * ConditionDecision. */ @Deprecated public void addAdvices(ConditionDecision decision) { if (decision != null) { Map> otherAdvices = decision.getAdvice(); if (otherAdvices != null && !otherAdvices.isEmpty()) { advice.putAll(otherAdvices); } } } /** * New decision builder. * * @param satisfied * whether the decision represents a successful evaluation or not * * @return new builder instance */ public static Builder newBuilder(boolean satisfied) { return new Builder(satisfied); } /** * New builder representing a satisfied. * * @return new builder instance */ public static Builder newSuccessBuilder() { return new Builder(true); } /** * New builder representing a failure. * * @return new builder instance */ public static Builder newFailureBuilder() { return new Builder(false); } /** * Builder to help construct decisions. */ public static final class Builder { private final boolean satisfied; private Map> advice; private Map> responseAttributes; private long timeToLive; private Builder(boolean success) { this.satisfied = success; advice = new HashMap<>(); responseAttributes = new HashMap<>(); timeToLive = Long.MAX_VALUE; } /** * Sets the advice. * * @param advice * the advice * * @return this builder instance */ public Builder setAdvice(Map> advice) { Reject.ifNull(advice); this.advice = advice; return this; } /** * Sets the response attributes. * * @param responseAttributes * the response attributes * * @return this builder instance */ public Builder setResponseAttributes(Map> responseAttributes) { Reject.ifNull(responseAttributes); this.responseAttributes = responseAttributes; return this; } /** * Sets the time to live. * * @param timeToLive * the time to live * * @return this builder instance */ public Builder setTimeToLive(long timeToLive) { Reject.ifTrue(timeToLive < 0); this.timeToLive = timeToLive; return this; } /** * Builds the decision. * * @return the decision instance */ public ConditionDecision build() { return new ConditionDecision(this); } } }