VariantValue.java revision 56ed4167b942ec265f9cee70ac4d71d10b3835ce
1/*
2 * Copyright (C) 2010 Google Inc.
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
17package com.google.clearsilver.jsilver.values;
18
19import com.google.clearsilver.jsilver.autoescape.EscapeMode;
20import com.google.clearsilver.jsilver.data.TypeConverter;
21
22/**
23 * Base class for values of variant types (i.e. those that can be treated as different types at
24 * runtime - e.g. "33").
25 *
26 * @see Value
27 */
28abstract class VariantValue extends Value {
29
30  private static final String EMPTY = "";
31
32  VariantValue(EscapeMode escapeMode, boolean partiallyEscaped) {
33    super(escapeMode, partiallyEscaped);
34  }
35
36  protected abstract String value();
37
38  @Override
39  public boolean asBoolean() {
40    return TypeConverter.asBoolean(value());
41  }
42
43  @Override
44  public String asString() {
45    String value = value();
46    return value == null ? EMPTY : value;
47  }
48
49  @Override
50  public int asNumber() {
51    // TODO: Cache the result for constant values (or just get rid of this class)
52    return TypeConverter.asNumber(value());
53  }
54
55  @Override
56  public boolean isEmpty() {
57    return asString().isEmpty();
58  }
59
60}
61