AbstractMVCGroup.java
001 /*
002  * Copyright 2008-2014 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package org.codehaus.griffon.runtime.core.mvc;
017 
018 import griffon.core.artifact.*;
019 import griffon.core.mvc.MVCGroup;
020 import griffon.core.mvc.MVCGroupConfiguration;
021 import griffon.core.mvc.MVCGroupManager;
022 
023 import javax.annotation.Nonnull;
024 import javax.annotation.Nullable;
025 import java.util.LinkedHashMap;
026 import java.util.Map;
027 import java.util.UUID;
028 
029 import static griffon.util.GriffonClassUtils.requireState;
030 import static griffon.util.GriffonNameUtils.isBlank;
031 import static griffon.util.GriffonNameUtils.requireNonBlank;
032 import static java.util.Collections.unmodifiableMap;
033 import static java.util.Objects.requireNonNull;
034 
035 /**
036  * Base implementation of the {@code MVCGroup} interface
037  *
038  @author Andres Almiray
039  @since 2.0.0
040  */
041 public abstract class AbstractMVCGroup implements MVCGroup {
042     protected final MVCGroupManager mvcGroupManager;
043     protected final MVCGroupConfiguration configuration;
044     protected final String mvcId;
045     protected final Map<String, Object> members = new LinkedHashMap<>();
046     private boolean alive;
047     private final Object[] lock = new Object[0];
048 
049     public AbstractMVCGroup(@Nonnull MVCGroupManager mvcGroupManager, @Nonnull MVCGroupConfiguration configuration, @Nullable String mvcId, @Nonnull Map<String, Object> members) {
050         this.mvcGroupManager = requireNonNull(mvcGroupManager, "Argument 'mvcGroupManager' must not be null");
051         this.configuration = requireNonNull(configuration, "Argument 'configuration' must not be null");
052         this.mvcId = isBlank(mvcId? configuration.getMvcType() "-" + UUID.randomUUID().toString() : mvcId;
053         this.members.putAll(requireNonNull(members, "Argument 'members' must not be null"));
054         this.alive = true;
055     }
056 
057     @Nonnull
058     public MVCGroupManager getMvcGroupManager() {
059         return mvcGroupManager;
060     }
061 
062     @Nonnull
063     @Override
064     public MVCGroupConfiguration getConfiguration() {
065         return configuration;
066     }
067 
068     @Nonnull
069     @Override
070     public String getMvcType() {
071         return configuration.getMvcType();
072     }
073 
074     @Nonnull
075     @Override
076     public String getMvcId() {
077         return mvcId;
078     }
079 
080     @Nullable
081     @Override
082     public GriffonModel getModel() {
083         return (GriffonModelgetMember(GriffonModelClass.TYPE);
084     }
085 
086     @Nullable
087     @Override
088     public GriffonView getView() {
089         return (GriffonViewgetMember(GriffonViewClass.TYPE);
090     }
091 
092     @Nullable
093     @Override
094     public GriffonController getController() {
095         return (GriffonControllergetMember(GriffonControllerClass.TYPE);
096     }
097 
098     @Nullable
099     @Override
100     public Object getMember(@Nonnull String name) {
101         requireNonBlank(name, "Argument 'name' must not be blank");
102         checkIfAlive();
103         return members.get(name);
104     }
105 
106     @Nonnull
107     @Override
108     public Map<String, Object> getMembers() {
109         checkIfAlive();
110         return unmodifiableMap(members);
111     }
112 
113     @Override
114     public void destroy() {
115         if (isAlive()) {
116             mvcGroupManager.destroyMVCGroup(mvcId);
117             members.clear();
118             synchronized (lock) {
119                 alive = false;
120             }
121         }
122     }
123 
124     @Override
125     public boolean isAlive() {
126         synchronized (lock) {
127             return alive;
128         }
129     }
130 
131     protected void checkIfAlive() {
132         requireState(isAlive()"Group " + getMvcType() ":" + mvcId + " has been destroyed already.");
133     }
134 }