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.Collections;
020import java.util.Objects;
021
022import org.apache.wicket.request.Response;
023import org.apache.wicket.util.lang.Args;
024
025/**
026 * Free form {@code HeaderItem}. No checks are performed on what is added to the header.
027 * 
028 * @author papegaaij
029 */
030public class StringHeaderItem extends HeaderItem
031{
032        /**
033         * Creates a {@link StringHeaderItem} for the snippet.
034         * 
035         * @param string
036         *            string to be rendered to head
037         * 
038         * @return A newly created {@link StringHeaderItem}.
039         */
040        public static StringHeaderItem forString(CharSequence string)
041        {
042                return new StringHeaderItem(string);
043        }
044
045        private final CharSequence string;
046
047        /**
048         * Construct.
049         * 
050         * @param string
051         */
052        public StringHeaderItem(CharSequence string)
053        {
054                this.string = Args.notNull(string, "string");
055        }
056
057        /**
058         * @return the string that gets added to the header.
059         */
060        public CharSequence getString()
061        {
062                return string;
063        }
064
065        @Override
066        public void render(Response response)
067        {
068                response.write(getString());
069        }
070
071        @Override
072        public Iterable<?> getRenderTokens()
073        {
074                return Collections.singletonList(getString());
075        }
076
077        @Override
078        public String toString()
079        {
080                return "StringHeaderItem(" + getString() + ")";
081        }
082
083        @Override
084        public boolean equals(Object o)
085        {
086                if (this == o) return true;
087                if (o == null || getClass() != o.getClass()) return false;
088                StringHeaderItem that = (StringHeaderItem) o;
089                return Objects.equals(string, that.string);
090        }
091
092        @Override
093        public int hashCode()
094        {
095                return Objects.hash(string);
096        }
097}