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.commons.flatfile;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.lang3.Validate;
023
024/**
025 * Support and basic field options.
026 * @version $Revision: 1301248 $ $Date: 2012-03-15 17:20:49 -0500 (Thu, 15 Mar 2012) $
027 */
028public abstract class FieldSupport extends EntitySupport {
029
030    /** Serialization version */
031    private static final long serialVersionUID = -8940832296518637934L;
032
033    /**
034     * Overflow behavior enumerated type.
035     */
036    public enum Overflow implements FieldOption {
037        /** Error on overflow */
038        ERROR() {
039            /**
040             * {@inheritDoc}
041             */
042            protected void check(byte[] b, int len) {
043                Validate.isTrue(b.length <= len,
044                    "Value '%s' too large for field (%s)", new String(b), len);
045            }
046        },
047
048        /** Truncate on overflow (same as IGNORE) */
049        TRUNCATE,
050
051        /** Ignore overflow (same as TRUNCATE) */
052        IGNORE;
053
054        /**
055         * Check the overflow of the field
056         * @param b value
057         * @param len field length
058         */
059        protected void check(byte[] b, int len) {
060        }
061    }
062
063    /**
064     * Underflow behavior enumerated type.
065     */
066    public enum Underflow implements FieldOption {
067        /** Error on Underflow */
068        ERROR() {
069            /**
070             * {@inheritDoc}
071             */
072            protected void check(byte[] b, int len) {
073                Validate.isTrue(b.length >= len,
074                    "Value '%s' too small for field (%s)", new String(b), len);
075            }
076        },
077
078        /** Ignore Underflow */
079        IGNORE;
080
081        /**
082         * Check the underflow of the field
083         * @param b value
084         * @param len field length
085         */
086        protected void check(byte[] b, int len) {
087        }
088    }
089
090    /**
091     * Convenience #readFrom alias.
092     * @param is InputStream for <code>readFrom</code>.
093     */
094    protected void dieOnExceptionRead(InputStream is) {
095        try {
096            readFrom(is);
097        } catch (IOException e) {
098            throw new RuntimeException(e);
099        }
100    }
101}