01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2008-2017 the original author or authors.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.codehaus.griffon.runtime.core.mvc;
19
20 import griffon.core.artifact.GriffonController;
21 import griffon.core.artifact.GriffonModel;
22 import griffon.core.artifact.GriffonView;
23 import griffon.core.mvc.MVCGroup;
24 import griffon.core.mvc.TypedMVCGroup;
25
26 import javax.annotation.Nonnull;
27
28 import static griffon.util.GriffonClassUtils.requireState;
29
30 /**
31 * @author Andres Almiray
32 * @since 2.11.0
33 */
34 public abstract class AbstractTypedMVCGroup<M extends GriffonModel, V extends GriffonView, C extends GriffonController> extends DelegatingMVCGroup implements TypedMVCGroup<M, V, C> {
35 public AbstractTypedMVCGroup(@Nonnull MVCGroup delegate) {
36 super(delegate);
37
38 GriffonModel model = getModel();
39 requireState(model != null, "MVC member 'model' must not be null");
40 try {
41 M m = (M) model;
42 } catch (ClassCastException cce) {
43 throw new IllegalStateException("Expected 'model' is not of the right type for " + getClass().getName());
44 }
45
46 GriffonView view = getView();
47 requireState(view != null, "MVC member 'view' must not be null");
48 try {
49 V v = (V) view;
50 } catch (ClassCastException cce) {
51 throw new IllegalStateException("Expected 'view' is not of the right type for " + getClass().getName());
52 }
53
54 GriffonController controller = getController();
55 requireState(controller != null, "MVC member 'controller' must not be null");
56 try {
57 C c = (C) controller;
58 } catch (ClassCastException cce) {
59 throw new IllegalStateException("Expected 'controller' is not of the right type for " + getClass().getName());
60 }
61 }
62
63 @Nonnull
64 @Override
65 public M model() {
66 return (M) delegate().getModel();
67 }
68
69 @Nonnull
70 @Override
71 public V view() {
72 return (V) delegate().getView();
73 }
74
75 @Nonnull
76 @Override
77 public C controller() {
78 return (C) delegate().getController();
79 }
80 }
|