source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/CollectionListModel.java @ 4640

Last change on this file since 4640 was 4640, checked in by Twan Goosen, 10 years ago

added resources overview to the record page

File size: 1.8 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.wicket.model;
18
19import com.google.common.collect.Lists;
20import java.util.Collection;
21import java.util.List;
22import 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 */
31public 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        } else if (object == null) {
45            return null;
46        } else {
47            return Lists.newArrayList(object);
48        }
49    }
50
51    @Override
52    public void setObject(List<T> object) {
53        collectionModel.setObject(object);
54    }
55
56    @Override
57    public void detach() {
58        collectionModel.detach();
59    }
60
61}
Note: See TracBrowser for help on using the repository browser.