001    /**   
002     * Copyright 2011 The Buzz Media, LLC
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package com.thebuzzmedia.cloudfront;
017    
018    public abstract class AbstractLogEntry implements ILogEntry {
019            protected Type type;
020            protected char[][] values;
021    
022            public AbstractLogEntry(Type type, char[][] values)
023                            throws IllegalArgumentException {
024                    if (type == null)
025                            throw new IllegalArgumentException("type cannot be null");
026                    if (values == null || values.length == 0)
027                            throw new IllegalArgumentException(
028                                            "values cannot be null or empty and must represent an array of one char[] instance for each field value.");
029    
030                    this.type = type;
031                    this.values = values;
032            }
033    
034            public String toString() {
035                    StringBuilder params = new StringBuilder();
036    
037                    for (int i = 0; i < values.length; i++) {
038                            char[] v = values[i];
039    
040                            if (v != null)
041                                    params.append(v);
042    
043                            if (i < values.length - 1)
044                                    params.append(',');
045                    }
046    
047                    return this.getClass().getName() + "@" + hashCode() + "[type=" + type
048                                    + ", fieldCount=" + values.length + ", values={"
049                                    + params.toString() + "}]";
050            }
051    
052            public void reset() {
053                    for (int i = 0; i < values.length; i++)
054                            values[i] = null;
055            }
056    
057            public Type getType() {
058                    return type;
059            }
060    
061            public int getFieldCount() {
062                    return values.length;
063            }
064    
065            public char[] getFieldValue(int fieldIndex) throws IllegalArgumentException {
066                    if (fieldIndex < 0 || fieldIndex >= values.length)
067                            throw new IllegalArgumentException("fieldIndex [" + fieldIndex
068                                            + "] must be >= 0 and < getFieldCount() [" + values.length
069                                            + "]");
070    
071                    return values[fieldIndex];
072            }
073    
074            public char[][] getFieldValues() {
075                    return values;
076            }
077    
078            public void setFieldValue(int fieldIndex, char[] value)
079                            throws IllegalArgumentException {
080                    if (fieldIndex < 0 || fieldIndex >= values.length)
081                            throw new IllegalArgumentException("fieldIndex [" + fieldIndex
082                                            + "] must be >= 0 and < getFieldCount() [" + values.length
083                                            + "]");
084    
085                    // Convert all unusable or empty values to null
086                    if (value != null
087                                    && (value.length == 0 || (value.length == 1 && value[0] == EMPTY_VALUE_FLAG)))
088                            value = null;
089    
090                    values[fieldIndex] = value;
091            }
092    }