001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.repeater;
018
019import java.io.Serializable;
020import java.util.Comparator;
021
022import org.apache.wicket.markup.html.list.ListItem;
023import org.apache.wicket.model.IModel;
024
025
026/**
027 * Container that holds components in a RefreshingView. One Item represents one entire row of the
028 * view. Users should add all containing components to the Item instead of the view, this is
029 * accomplished by implementing refreshingView.populateItem(Item item).
030 * 
031 * @see RefreshingView
032 * 
033 * @author Igor Vaynberg (ivaynberg)
034 * 
035 * @param <T>
036 *            Model object type
037 */
038public class Item<T> extends ListItem<T>
039{
040        private static final long serialVersionUID = 1L;
041
042        /**
043         * Constructor
044         * 
045         * @param id
046         *            component id
047         * @param index
048         *            relative index of this item in the pageable view
049         * @param model
050         *            model for this item
051         */
052        public Item(final String id, int index, final IModel<T> model)
053        {
054                super(id, index, model);
055        }
056
057        /**
058         * Constructor
059         * 
060         * @param id
061         *            component id
062         * @param index
063         *            relative index of this item in the pageable view
064         */
065        public Item(final String id, int index)
066        {
067                super(id, index);
068        }
069
070        /**
071         * @return the primary key assigned to this item
072         */
073        public String getPrimaryKey()
074        {
075                return getId();
076        }
077
078        /**
079         * Comparator that compares Items by their index property
080         * 
081         * @author Igor Vaynberg (ivaynberg)
082         */
083        public static class IndexComparator implements Comparator<Item<?>>, Serializable
084        {
085                private static final long serialVersionUID = 1L;
086                private static final Comparator<Item<?>> instance = new IndexComparator();
087
088                /**
089                 * @return static instance of the comparator
090                 */
091                public static Comparator<Item<?>> getInstance()
092                {
093                        return instance;
094                }
095
096                /**
097                 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
098                 */
099                @Override
100                public int compare(Item<?> lhs, Item<?> rhs)
101                {
102                        long diff = lhs.getIndex() - rhs.getIndex();
103                        return diff == 0 ? 0 : diff > 0 ? 1 : -1;
104                }
105        }
106}