Skip to content

Package: AbstractTransformer

AbstractTransformer

nameinstructionbranchcomplexitylinemethod
AbstractTransformer()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createStateObject()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getLastResult(AttributeStorage)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getMemoryManager()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getNamePrefix()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getValue(AttributeStorage, Attribute, Object)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
obtainMemoryManager(AttributeStorage)
M: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
obtainStateObject(AttributeStorage)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
release(AttributeStorage)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
saveLastResult(AttributeStorage, TransformationResult)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setMemoryManager(MemoryManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
transform(AttributeStorage, Object)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly;
18:
19: import org.glassfish.grizzly.attributes.Attribute;
20: import org.glassfish.grizzly.attributes.AttributeBuilder;
21: import org.glassfish.grizzly.attributes.AttributeStorage;
22: import org.glassfish.grizzly.memory.MemoryManager;
23:
24: /**
25: *
26: * @author Alexey Stashok
27: */
28: public abstract class AbstractTransformer<K, L> implements Transformer<K, L> {
29: protected final AttributeBuilder attributeBuilder = Grizzly.DEFAULT_ATTRIBUTE_BUILDER;
30:
31: protected final Attribute<LastResultAwareState<K, L>> stateAttr;
32:
33: private MemoryManager memoryManager;
34:
35: public AbstractTransformer() {
36: String namePrefix = getNamePrefix();
37:
38: stateAttr = attributeBuilder.createAttribute(namePrefix + ".state");
39: }
40:
41: protected String getNamePrefix() {
42: return getClass().getName();
43: }
44:
45: @Override
46: public final TransformationResult<K, L> transform(AttributeStorage storage, K input) throws TransformationException {
47: return saveLastResult(storage, transformImpl(storage, input));
48: }
49:
50: protected abstract TransformationResult<K, L> transformImpl(AttributeStorage storage, K input) throws TransformationException;
51:
52: @Override
53: public final TransformationResult<K, L> getLastResult(final AttributeStorage storage) {
54: final LastResultAwareState<K, L> state = stateAttr.get(storage);
55:• if (state != null) {
56: return state.getLastResult();
57: }
58:
59: return null;
60: }
61:
62: protected final TransformationResult<K, L> saveLastResult(final AttributeStorage storage, final TransformationResult<K, L> result) {
63: obtainStateObject(storage).setLastResult(result);
64: return result;
65: }
66:
67: @Override
68: public void release(AttributeStorage storage) {
69: stateAttr.remove(storage);
70: }
71:
72: protected MemoryManager obtainMemoryManager(AttributeStorage storage) {
73:• if (memoryManager != null) {
74: return memoryManager;
75: }
76:
77:• if (storage instanceof Connection) {
78: Connection connection = (Connection) storage;
79: return connection.getMemoryManager();
80: }
81:
82: return MemoryManager.DEFAULT_MEMORY_MANAGER;
83: }
84:
85: public MemoryManager getMemoryManager() {
86: return memoryManager;
87: }
88:
89: public void setMemoryManager(MemoryManager memoryManager) {
90: this.memoryManager = memoryManager;
91: }
92:
93: public static <T> T getValue(final AttributeStorage storage, final Attribute<T> attribute, final T defaultValue) {
94: final T value = attribute.get(storage);
95:• if (value != null) {
96: return value;
97: }
98:
99: return defaultValue;
100: }
101:
102: protected final LastResultAwareState<K, L> obtainStateObject(final AttributeStorage storage) {
103:
104: LastResultAwareState<K, L> value = stateAttr.get(storage);
105:• if (value == null) {
106: value = createStateObject();
107: stateAttr.set(storage, value);
108: }
109:
110: return value;
111: }
112:
113: protected LastResultAwareState<K, L> createStateObject() {
114: return new LastResultAwareState<>();
115: }
116:
117: public static class LastResultAwareState<K, L> {
118: protected TransformationResult<K, L> lastResult;
119:
120: public TransformationResult<K, L> getLastResult() {
121: return lastResult;
122: }
123:
124: public void setLastResult(TransformationResult<K, L> lastResult) {
125: this.lastResult = lastResult;
126: }
127: }
128: }