EMMA Coverage Report (generated Wed Aug 29 00:03:59 CDT 2007)
[all classes][org.fuwjax.jon.type]

COVERAGE SUMMARY FOR SOURCE FILE [ByteArrayType.java]

nameclass, %method, %block, %line, %
ByteArrayType.java100% (1/1)100% (10/10)100% (174/174)100% (34/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ByteArrayType100% (1/1)100% (10/10)100% (174/174)100% (34/34)
<static initializer> 100% (1/1)100% (6/6)100% (1/1)
ByteArrayType (): void 100% (1/1)100% (4/4)100% (2/2)
createByteArray (int, Object): byte [] 100% (1/1)100% (29/29)100% (6/6)
createInstance (CharSequence, Object): Object 100% (1/1)100% (62/62)100% (10/10)
getClassCastStrategy (): ClassCastStrategy 100% (1/1)100% (2/2)100% (1/1)
getLiteral (): Literal 100% (1/1)100% (2/2)100% (1/1)
getReferenceStrategy (): ReferenceStrategy 100% (1/1)100% (2/2)100% (1/1)
getSymbolSpec (): Symbol 100% (1/1)100% (2/2)100% (1/1)
isValidIndex (int, int, int): boolean 100% (1/1)100% (12/12)100% (1/1)
toString (Object): CharSequence 100% (1/1)100% (53/53)100% (10/10)

1/*
2 * This file is part of JON.
3 *
4 * JON is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * JON is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 * 
17 * Copyright 2007 Michael Doberenz
18 */
19package org.fuwjax.jon.type;
20 
21import org.fuwjax.jon.ClassCastStrategy;
22import org.fuwjax.jon.ObjectAccessException;
23import org.fuwjax.jon.ReferenceStrategy;
24import org.fuwjax.jon.accessor.Literal;
25import org.fuwjax.jon.accessor.LiteralAccessor;
26import org.fuwjax.jon.accessor.Symbol;
27import org.fuwjax.util.BaseConverter;
28 
29/**
30 * An abstraction layer over byte array.
31 * @author michaeldoberenz
32 */
33public class ByteArrayType extends AbstractIndirectType implements LiteralAccessor{
34        private static final BaseConverter CONVERTER = new BaseConverter(
35              "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$"); //$NON-NLS-1$
36        private static final int STRING_BLOCK_LENGTH = 4;
37        private static final int BYTE_BLOCK_LENGTH = 3;
38        private static final int BYTE_WIDTH = 8;
39        private static final int BYTE_MASK = (1 << BYTE_WIDTH) - 1;
40 
41        /**
42         * Creates an abstraction layer for byte[].
43         */
44        public ByteArrayType(){
45                super(byte[].class);
46        }
47 
48        public ClassCastStrategy getClassCastStrategy(){
49                return ClassCastStrategy.AlwaysCast;
50        }
51 
52        public ReferenceStrategy getReferenceStrategy(){
53                return ReferenceStrategy.AssignedReference;
54        }
55 
56        public Symbol getSymbolSpec(){
57                return Symbol.Literal;
58        }
59 
60        public Object createInstance(final CharSequence literal, final Object source) throws ObjectAccessException{
61                final int stringLength = literal.length();
62                final byte[] bytes = createByteArray(stringLength, source);
63                for(int stringPos = 0; stringPos < stringLength; stringPos += STRING_BLOCK_LENGTH){
64                        final int blockPos = stringPos / STRING_BLOCK_LENGTH * BYTE_BLOCK_LENGTH;
65                        final int stringBlockLength = Math.min(STRING_BLOCK_LENGTH, stringLength - stringPos);
66                        final CharSequence blockString = literal.subSequence(stringPos, stringPos + stringBlockLength);
67                        final int block = CONVERTER.toInt(blockString);
68                        for(int blockOffset = 0; blockOffset < stringBlockLength - 1; ++blockOffset){
69                                bytes[blockPos + blockOffset] = (byte)((block >> (blockOffset * BYTE_WIDTH)) & BYTE_MASK);
70                        }
71                }
72                return bytes;
73        }
74 
75        private byte[] createByteArray(final int length, final Object source){
76                if(source instanceof byte[]){
77                        return (byte[])source;
78                }
79                if(length <= 0){
80                        return new byte[0];
81                }
82                final int size = (length - 1) / STRING_BLOCK_LENGTH * BYTE_BLOCK_LENGTH + ((length - 1) % STRING_BLOCK_LENGTH);
83                return new byte[size];
84        }
85 
86        public Literal getLiteral(){
87                return Literal.String;
88        }
89 
90        public CharSequence toString(final Object object) throws ObjectAccessException{
91                final StringBuilder builder = new StringBuilder();
92                final byte[] bytes = (byte[])object;
93                for(int i = 0; i < bytes.length; i += BYTE_BLOCK_LENGTH){
94                        int block = 0;
95                        int blockLength = 0;
96                        for(; isValidIndex(bytes.length, i, blockLength); ++blockLength){
97                                block |= bytes[i + blockLength] << (BYTE_WIDTH * blockLength);
98                        }
99                        final CharSequence blockString = CONVERTER.toCharSequence(block, blockLength + 1);
100                        builder.append(blockString);
101                }
102                return builder;
103        }
104 
105        private static boolean isValidIndex(final int arrayLength, final int blockIndex, final int blockPosition){
106                return blockPosition < BYTE_BLOCK_LENGTH && blockPosition + blockIndex < arrayLength;
107        }
108}

[all classes][org.fuwjax.jon.type]
EMMA 2.0.5312 (C) Vladimir Roubtsov