FieldPort.java revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
1/*
2 * Copyright (C) 2011 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
18package android.filterfw.core;
19
20import java.lang.reflect.Field;
21
22/**
23 * @hide
24 */
25public class FieldPort extends InputPort {
26
27    protected Field mField;
28    protected boolean mHasFrame;
29    protected boolean mValueWaiting = false;
30    protected Object mValue;
31
32    public FieldPort(Filter filter, String name, Field field, boolean hasDefault) {
33        super(filter, name);
34        mField = field;
35        mHasFrame = hasDefault;
36    }
37
38    @Override
39    public void clear() {
40    }
41
42    @Override
43    public void pushFrame(Frame frame) {
44        setFieldFrame(frame, false);
45    }
46
47    @Override
48    public void setFrame(Frame frame) {
49        setFieldFrame(frame, true);
50    }
51
52    @Override
53    public Object getTarget() {
54        try {
55            return mField.get(mFilter);
56        } catch (IllegalAccessException e) {
57            return null;
58        }
59    }
60
61    @Override
62    public synchronized void transfer(FilterContext context) {
63        if (mValueWaiting) {
64            try {
65                mField.set(mFilter, mValue);
66            } catch (IllegalAccessException e) {
67                throw new RuntimeException(
68                    "Access to field '" + mField.getName() + "' was denied!");
69            }
70            mValueWaiting = false;
71            if (context != null) {
72                mFilter.notifyFieldPortValueUpdated(mName, context);
73            }
74        }
75    }
76
77    @Override
78    public synchronized Frame pullFrame() {
79        throw new RuntimeException("Cannot pull frame on " + this + "!");
80    }
81
82    @Override
83    public synchronized boolean hasFrame() {
84        return mHasFrame;
85    }
86
87    @Override
88    public synchronized boolean acceptsFrame() {
89        return !mValueWaiting;
90    }
91
92    @Override
93    public String toString() {
94        return "field " + super.toString();
95    }
96
97    protected synchronized void setFieldFrame(Frame frame, boolean isAssignment) {
98        assertPortIsOpen();
99        checkFrameType(frame, isAssignment);
100
101        // Store the object value
102        Object value = frame.getObjectValue();
103        if ((value == null && mValue != null) || !value.equals(mValue)) {
104            mValue = value;
105            mValueWaiting = true;
106        }
107
108        // Since a frame was set, mark this port as having a frame to pull
109        mHasFrame = true;
110    }
111}
112