Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FieldSupport |
|
| 1.4;1.4 | ||||
FieldSupport$1 |
|
| 1.4;1.4 | ||||
FieldSupport$Overflow |
|
| 1.4;1.4 | ||||
FieldSupport$Overflow$1 |
|
| 1.4;1.4 | ||||
FieldSupport$Underflow |
|
| 1.4;1.4 | ||||
FieldSupport$Underflow$1 |
|
| 1.4;1.4 |
1 | 2 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.flatfile; | |
18 | ||
19 | import java.io.IOException; | |
20 | import java.io.InputStream; | |
21 | ||
22 | import org.apache.commons.lang3.Validate; | |
23 | ||
24 | /** | |
25 | * Support and basic field options. | |
26 | * @version $Revision: 1301248 $ $Date: 2012-03-15 17:20:49 -0500 (Thu, 15 Mar 2012) $ | |
27 | */ | |
28 | 768 | public abstract class FieldSupport extends EntitySupport { |
29 | ||
30 | /** Serialization version */ | |
31 | private static final long serialVersionUID = -8940832296518637934L; | |
32 | ||
33 | /** | |
34 | * Overflow behavior enumerated type. | |
35 | */ | |
36 | 34 | public enum Overflow implements FieldOption { |
37 | 2 | /** Error on overflow */ |
38 | 6 | ERROR() { |
39 | /** | |
40 | * {@inheritDoc} | |
41 | */ | |
42 | protected void check(byte[] b, int len) { | |
43 | 744 | Validate.isTrue(b.length <= len, |
44 | 186 | "Value '%s' too large for field (%s)", new String(b), len); |
45 | 516 | } |
46 | }, | |
47 | ||
48 | 2 | /** Truncate on overflow (same as IGNORE) */ |
49 | 6 | TRUNCATE, |
50 | ||
51 | 2 | /** Ignore overflow (same as TRUNCATE) */ |
52 | 6 | IGNORE; |
53 | ||
54 | /** | |
55 | * Check the overflow of the field | |
56 | * @param b value | |
57 | * @param len field length | |
58 | */ | |
59 | protected void check(byte[] b, int len) { | |
60 | 132 | } |
61 | } | |
62 | ||
63 | /** | |
64 | * Underflow behavior enumerated type. | |
65 | */ | |
66 | 24 | public enum Underflow implements FieldOption { |
67 | 2 | /** Error on Underflow */ |
68 | 6 | ERROR() { |
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | protected void check(byte[] b, int len) { | |
73 | 512 | Validate.isTrue(b.length >= len, |
74 | 128 | "Value '%s' too small for field (%s)", new String(b), len); |
75 | 372 | } |
76 | }, | |
77 | ||
78 | 2 | /** Ignore Underflow */ |
79 | 6 | IGNORE; |
80 | ||
81 | /** | |
82 | * Check the underflow of the field | |
83 | * @param b value | |
84 | * @param len field length | |
85 | */ | |
86 | protected void check(byte[] b, int len) { | |
87 | 0 | } |
88 | } | |
89 | ||
90 | /** | |
91 | * Convenience #readFrom alias. | |
92 | * @param is InputStream for <code>readFrom</code>. | |
93 | */ | |
94 | protected void dieOnExceptionRead(InputStream is) { | |
95 | try { | |
96 | 726 | readFrom(is); |
97 | 242 | } catch (IOException e) { |
98 | 0 | throw new RuntimeException(e); |
99 | 484 | } |
100 | 726 | } |
101 | } |