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.head;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Objects;
022
023import org.apache.wicket.request.Response;
024
025/**
026 * {@link HeaderItem} that has priority over other header items. {@code PriorityHeaderItem}s
027 * rendered parent-first at the beginning of the header. Dependencies of a
028 * {@code PriorityHeaderItem} also have priority.
029 * 
030 * @author papegaaij
031 */
032public class PriorityHeaderItem extends HeaderItem implements IWrappedHeaderItem
033{
034        private final HeaderItem wrapped;
035
036        /**
037         * Construct.
038         * 
039         * @param wrapped
040         *            the actual {@link HeaderItem} that should have priority
041         */
042        public PriorityHeaderItem(HeaderItem wrapped)
043        {
044                this.wrapped = wrapped;
045        }
046
047        /**
048         * @return the actual {@link HeaderItem}
049         */
050        @Override
051        public HeaderItem getWrapped()
052        {
053                return wrapped;
054        }
055
056        @Override
057        public PriorityHeaderItem wrap(HeaderItem item)
058        {
059                return new PriorityHeaderItem(item);
060        }
061
062        @Override
063        public Iterable<?> getRenderTokens()
064        {
065                return getWrapped().getRenderTokens();
066        }
067
068        @Override
069        public void render(Response response)
070        {
071                getWrapped().render(response);
072        }
073
074        @Override
075        public List<HeaderItem> getDependencies()
076        {
077                List<PriorityHeaderItem> ret = new ArrayList<>();
078                for (HeaderItem curDependency : getWrapped().getDependencies())
079                {
080                        ret.add(wrap(curDependency));
081                }
082                List<HeaderItem> dependencies = super.getDependencies();
083                dependencies.addAll(ret);
084                return dependencies;
085        }
086
087        @Override
088        public Iterable<? extends HeaderItem> getProvidedResources()
089        {
090                return getWrapped().getProvidedResources();
091        }
092
093        @Override
094        public boolean equals(Object o)
095        {
096                if (this == o) return true;
097                if (o == null || getClass() != o.getClass()) return false;
098                PriorityHeaderItem that = (PriorityHeaderItem) o;
099                return Objects.equals(wrapped, that.wrapped);
100        }
101
102        @Override
103        public int hashCode()
104        {
105                return Objects.hash(wrapped);
106        }
107
108        @Override
109        public String toString()
110        {
111                return "PriorityHeaderItem(" + getWrapped() + ")";
112        }
113}