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.util.resource;
018
019import java.time.Instant;
020import org.apache.wicket.util.string.AppendingStringBuffer;
021
022/**
023 * A string resource that can be appended to.
024 * 
025 * @author Jonathan Locke
026 */
027public class StringBufferResourceStream extends AbstractStringResourceStream
028{
029        private static final long serialVersionUID = 1L;
030
031        /** Stylesheet information */
032        private final AppendingStringBuffer buffer = new AppendingStringBuffer(128);
033
034        /**
035         * Constructor.
036         */
037        public StringBufferResourceStream()
038        {
039        }
040
041        /**
042         * Constructor.
043         * 
044         * @param contentType
045         *            The mime type of this resource, such as "image/jpeg" or "text/html"
046         */
047        public StringBufferResourceStream(final String contentType)
048        {
049                super(contentType);
050        }
051
052        /**
053         * Adds to this string buffer resource
054         * 
055         * @param s
056         *            The string to add
057         * @return this for chaining
058         */
059        public StringBufferResourceStream append(final CharSequence s)
060        {
061                buffer.append(s);
062                setLastModified(Instant.now());
063                return this;
064        }
065
066        /**
067         * Prepends to this string buffer resource
068         * 
069         * @param s
070         *            The string to prepend
071         * @return this for chaining
072         */
073        public StringBufferResourceStream prepend(final CharSequence s)
074        {
075                buffer.insert(0, s);
076                setLastModified(Instant.now());
077                return this;
078        }
079
080        /**
081         * Clears the string buffer resource.
082         * 
083         * @return this for chaining
084         */
085        public StringBufferResourceStream clear()
086        {
087                buffer.delete(0, buffer.length());
088                return this;
089        }
090
091        /**
092         * @see org.apache.wicket.util.resource.AbstractStringResourceStream#getString()
093         */
094        @Override
095        protected String getString()
096        {
097                return buffer.toString();
098        }
099}