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.extensions.markup.html.repeater.util;
018
019import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortState;
020import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
021import org.apache.wicket.util.io.IClusterable;
022import org.apache.wicket.util.lang.Args;
023
024/**
025 * Implementation of ISortState that can keep track of sort information for a single property.
026 * 
027 * @author Igor Vaynberg (ivaynberg)
028 * @param <T>
029 *            the type of the sort property
030 * 
031 */
032public class SingleSortState<T> implements ISortState<T>, IClusterable
033{
034        private static final long serialVersionUID = 1L;
035
036        SortParam<T> param;
037
038        @Override
039        public void setPropertySortOrder(final T property, final SortOrder order)
040        {
041                Args.notNull(property, "property");
042                Args.notNull(order, "order");
043
044                if (order == SortOrder.NONE)
045                {
046                        if ((param != null) && property.equals(param.getProperty()))
047                        {
048                                param = null;
049                        }
050                }
051                else
052                {
053                        param = new SortParam<>(property, order == SortOrder.ASCENDING);
054                }
055        }
056
057        @Override
058        public SortOrder getPropertySortOrder(final T property)
059        {
060                Args.notNull(property, "property");
061
062                if ((param == null) || (param.getProperty().equals(property) == false))
063                {
064                        return SortOrder.NONE;
065                }
066                return param.isAscending() ? SortOrder.ASCENDING : SortOrder.DESCENDING;
067        }
068
069        /**
070         * @return current sort state
071         */
072        public SortParam<T> getSort()
073        {
074                return param;
075        }
076
077        /**
078         * Sets the current sort state
079         * 
080         * @param param
081         *            parameter containing new sorting information
082         */
083        public void setSort(final SortParam<T> param)
084        {
085                this.param = param;
086        }
087
088        @Override
089        public String toString()
090        {
091                return "[SingleSortState sort=" + ((param == null) ? "null" : param.toString()) + ']';
092        }
093
094}