| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.tools.chunkio.processor; |
| 18 |
|
| 19 |
import com.android.tools.chunkio.Chunk; |
| 20 |
|
| 21 |
import javax.lang.model.element.VariableElement; |
| 22 |
import javax.lang.model.type.TypeMirror; |
| 23 |
|
| 24 |
/** |
| 25 |
* Simple delegate for {@link Chunk} annotations. This |
| 26 |
* delegate also holds the name and the type of the field |
| 27 |
* that was annotated with the chunk. |
| 28 |
*/ |
| 29 |
public class FieldChunk { |
| 30 |
/** The chunk found on a field. */ |
| 31 |
public final Chunk chunk; |
| 32 |
/** Name of the annotated field. */ |
| 33 |
public final String name; |
| 34 |
/** Type of the annotated field. */ |
| 35 |
public final TypeMirror type; |
| 36 |
|
| 37 |
private FieldChunk(Chunk chunk, String name, TypeMirror type) { |
| 38 |
this.chunk = chunk; |
| 39 |
this.name = name; |
| 40 |
this.type = type; |
| 41 |
} |
| 42 |
|
| 43 |
public boolean hasSize() { |
| 44 |
return size() >= 0 || !dynamicSize().isEmpty(); |
| 45 |
} |
| 46 |
|
| 47 |
boolean hasDynamicSize() { |
| 48 |
return !dynamicSize().isEmpty(); |
| 49 |
} |
| 50 |
|
| 51 |
public boolean hasByteCount() { |
| 52 |
return byteCount() > 0 || !dynamicByteCount().isEmpty(); |
| 53 |
} |
| 54 |
|
| 55 |
public boolean hasDynamicByteCount() { |
| 56 |
return !dynamicByteCount().isEmpty(); |
| 57 |
} |
| 58 |
|
| 59 |
public long byteCount() { |
| 60 |
return chunk.byteCount(); |
| 61 |
} |
| 62 |
|
| 63 |
public String encoding() { |
| 64 |
return chunk.encoding(); |
| 65 |
} |
| 66 |
|
| 67 |
public int bufferSize() { |
| 68 |
return chunk.bufferSize(); |
| 69 |
} |
| 70 |
|
| 71 |
String dynamicSize() { |
| 72 |
return chunk.dynamicSize(); |
| 73 |
} |
| 74 |
|
| 75 |
public String key() { |
| 76 |
return chunk.key(); |
| 77 |
} |
| 78 |
|
| 79 |
String dynamicByteCount() { |
| 80 |
return chunk.dynamicByteCount(); |
| 81 |
} |
| 82 |
|
| 83 |
Class<?>[] byteCountParams() { |
| 84 |
return chunk.byteCountParams(); |
| 85 |
} |
| 86 |
|
| 87 |
public int size() { |
| 88 |
return chunk.size(); |
| 89 |
} |
| 90 |
|
| 91 |
Class<?>[] sizeParams() { |
| 92 |
return chunk.sizeParams(); |
| 93 |
} |
| 94 |
|
| 95 |
Chunk.Case[] switchType() { |
| 96 |
return chunk.switchType(); |
| 97 |
} |
| 98 |
|
| 99 |
String match() { |
| 100 |
return chunk.match(); |
| 101 |
} |
| 102 |
|
| 103 |
String readIf() { |
| 104 |
return chunk.readIf(); |
| 105 |
} |
| 106 |
|
| 107 |
Class<?>[] readIfParams() { |
| 108 |
return chunk.readIfParams(); |
| 109 |
} |
| 110 |
|
| 111 |
String stopIf() { |
| 112 |
return chunk.stopIf(); |
| 113 |
} |
| 114 |
|
| 115 |
Class<?>[] stopIfParams() { |
| 116 |
return chunk.stopIfParams(); |
| 117 |
} |
| 118 |
|
| 119 |
boolean debug() { |
| 120 |
return chunk.debug(); |
| 121 |
} |
| 122 |
|
| 123 |
FieldChunk derive(String name) { |
| 124 |
return new FieldChunk(this.chunk, name, this.type); |
| 125 |
} |
| 126 |
|
| 127 |
static FieldChunk from(Chunk chunk, VariableElement field) { |
| 128 |
return new FieldChunk(chunk, field.getSimpleName().toString(), field.asType()); |
| 129 |
} |
| 130 |
} |