001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.consent;
021
022import java.util.ArrayList;
023import java.util.List;
024
025public class InteractionResultSet {
026
027    private final List<InteractionResult> results = new ArrayList<InteractionResult>();
028    private InteractionResult firstResult = null;
029
030    public InteractionResultSet() {
031    }
032
033    public InteractionResultSet add(final InteractionResult result) {
034        if (firstResult == null) {
035            firstResult = result;
036        }
037        this.results.add(result);
038        return this;
039    }
040
041    /**
042     * Empty only if all the {@link #add(InteractionResult) contained}
043     * {@link InteractionResult}s are also
044     * {@link InteractionResult#isNotVetoing() empty}.
045     */
046    public boolean isAllowed() {
047        return !isVetoed();
048    }
049
050    /**
051     * Vetoed if any of the {@link #add(InteractionResult) contained}
052     * {@link InteractionResult}s are also {@link InteractionResult#isVetoing()
053     * not empty}.
054     * 
055     * @return
056     */
057    public boolean isVetoed() {
058        for (final InteractionResult result : results) {
059            if (result.isVetoing()) {
060                return true;
061            }
062        }
063        return false;
064    }
065
066    /**
067     * Returns the {@link Consent} corresponding to
068     * {@link #getInteractionResult()}, or an {@link Allow} if there have been
069     * no {@link InteractionResult}s {@link #add(InteractionResult) added}.
070     * 
071     * @return
072     */
073    public Consent createConsent() {
074        final InteractionResult interactionResult = getInteractionResult();
075        if (interactionResult == null) {
076            return Allow.DEFAULT;
077        }
078        return interactionResult.createConsent();
079    }
080
081    /**
082     * Returns the &quot;best&quot; contained {@link InteractionResult}.
083     * 
084     * <p>
085     * This will be the first {@link InteractionResult} that has vetoed the
086     * interaction, or the first {@link InteractionResult}
087     * {@link #add(InteractionResult) added} if none have vetoed.
088     * 
089     * @return
090     */
091    public InteractionResult getInteractionResult() {
092        for (final InteractionResult result : results) {
093            if (!result.isNotVetoing()) {
094                return result;
095            }
096        }
097        return firstResult != null ? firstResult : null;
098    }
099
100    @Override
101    public String toString() {
102        return super.toString();
103    }
104}