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.progmodel.facets.collections.clear;
021
022import java.lang.reflect.Method;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.List;
026
027import org.apache.isis.applib.DomainObjectContainer;
028import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
029import org.apache.isis.core.metamodel.adapter.ObjectDirtier;
030import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
031import org.apache.isis.core.metamodel.adapter.util.AdapterInvokeUtils;
032import org.apache.isis.core.metamodel.facetapi.FacetHolder;
033import org.apache.isis.core.metamodel.facets.ImperativeFacet;
034import org.apache.isis.core.metamodel.facets.ImperativeFacet.Intent;
035import org.apache.isis.core.metamodel.facets.collections.modify.CollectionClearFacetAbstract;
036
037public class CollectionClearFacetViaAccessor extends CollectionClearFacetAbstract implements ImperativeFacet {
038
039    private final Method method;
040    private final AdapterManager adapterManager;
041    private final ObjectDirtier objectDirtier;
042
043    public CollectionClearFacetViaAccessor(final Method method, final FacetHolder holder, final AdapterManager adapterManager, final ObjectDirtier objectDirtier) {
044        super(holder);
045        this.method = method;
046        this.adapterManager = adapterManager;
047        this.objectDirtier = objectDirtier;
048    }
049
050    /**
051     * Returns a singleton list of the {@link Method} provided in the
052     * constructor.
053     */
054    @Override
055    public List<Method> getMethods() {
056        return Collections.singletonList(method);
057    }
058
059    @Override
060    public Intent getIntent(final Method method) {
061        return Intent.ACCESSOR;
062    }
063
064    @Override
065    public boolean impliesResolve() {
066        return true;
067    }
068
069    /**
070     * Bytecode cannot automatically call
071     * {@link DomainObjectContainer#objectChanged(Object)} because cannot
072     * distinguish whether interacting with accessor to read it or to modify its
073     * contents.
074     */
075    @Override
076    public boolean impliesObjectChanged() {
077        return false;
078    }
079
080    @Override
081    public void clear(final ObjectAdapter owningAdapter) {
082        final Collection<?> collection = (Collection<?>) AdapterInvokeUtils.invoke(method, owningAdapter);
083        collection.clear();
084        final ObjectAdapter adapter = getAdapterManager().getAdapterFor(owningAdapter);
085        getObjectDirtier().objectChanged(adapter);
086    }
087
088    @Override
089    protected String toStringValues() {
090        return "method=" + method;
091    }
092
093    // /////////////////////////////////////////////////////////
094    // Dependencies (from constructor)
095    // /////////////////////////////////////////////////////////
096
097    protected AdapterManager getAdapterManager() {
098        return adapterManager;
099    }
100
101    protected ObjectDirtier getObjectDirtier() {
102        return objectDirtier;
103    }
104
105}