1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.model; |
---|
18 | |
---|
19 | import com.google.common.collect.Lists; |
---|
20 | import java.util.Collection; |
---|
21 | import java.util.List; |
---|
22 | import org.apache.wicket.model.IModel; |
---|
23 | |
---|
24 | /** |
---|
25 | * Wraps a collection model to always return a list (passthrough if model object |
---|
26 | * is already a list, creates a new array list instead if needed) |
---|
27 | * |
---|
28 | * @author twagoo |
---|
29 | * @param <T> type of collection |
---|
30 | */ |
---|
31 | public class CollectionListModel<T> implements IModel<List<T>> { |
---|
32 | |
---|
33 | private final IModel<Collection<T>> collectionModel; |
---|
34 | |
---|
35 | public CollectionListModel(IModel<Collection<T>> collectionModel) { |
---|
36 | this.collectionModel = collectionModel; |
---|
37 | } |
---|
38 | |
---|
39 | @Override |
---|
40 | public List<T> getObject() { |
---|
41 | final Collection object = collectionModel.getObject(); |
---|
42 | if (object instanceof List) { |
---|
43 | return (List<T>) object; |
---|
44 | } |
---|
45 | return Lists.newArrayList(object); |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public void setObject(List<T> object) { |
---|
50 | collectionModel.setObject(object); |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | public void detach() { |
---|
55 | collectionModel.detach(); |
---|
56 | } |
---|
57 | |
---|
58 | } |
---|