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