1/*
2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package com.sun.beans.decoder;
26
27/**
28 * This is base class that simplifies access to entities (fields or properties).
29 * The {@code name} attribute specifies the name of the accessible entity.
30 * The element defines getter if it contains no argument
31 * or setter if it contains one argument.
32 *
33 * @since 1.7
34 *
35 * @author Sergey A. Malenkov
36 */
37abstract class AccessorElementHandler extends ElementHandler {
38    private String name;
39    private ValueObject value;
40
41    /**
42     * Parses attributes of the element.
43     * The following attributes are supported:
44     * <dl>
45     * <dt>name
46     * <dd>the name of the accessible entity
47     * <dt>id
48     * <dd>the identifier of the variable that is intended to store the result
49     * </dl>
50     *
51     * @param name   the attribute name
52     * @param value  the attribute value
53     */
54    @Override
55    public void addAttribute(String name, String value) {
56        if (name.equals("name")) { // NON-NLS: the attribute name
57            this.name = value;
58        } else {
59            super.addAttribute(name, value);
60        }
61    }
62
63    /**
64     * Adds the argument that is used to set the value of this element.
65     *
66     * @param argument  the value of the element that contained in this one
67     */
68    @Override
69    protected final void addArgument(Object argument) {
70        if (this.value != null) {
71            throw new IllegalStateException("Could not add argument to evaluated element");
72        }
73        setValue(this.name, argument);
74        this.value = ValueObjectImpl.VOID;
75    }
76
77    /**
78     * Returns the value of this element.
79     *
80     * @return the value of this element
81     */
82    @Override
83    protected final ValueObject getValueObject() {
84        if (this.value == null) {
85            this.value = ValueObjectImpl.create(getValue(this.name));
86        }
87        return this.value;
88    }
89
90    /**
91     * Returns the value of the entity with specified {@code name}.
92     *
93     * @param name  the name of the accessible entity
94     * @return the value of the specified entity
95     */
96    protected abstract Object getValue(String name);
97
98    /**
99     * Sets the new value for the entity with specified {@code name}.
100     *
101     * @param name   the name of the accessible entity
102     * @param value  the new value for the specified entity
103     */
104    protected abstract void setValue(String name, Object value);
105}
106