summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/util/config/ConfigChecker.java
blob: 63a21e5aedf8e11b5b06f3e4f8da9a535555259c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.sched.util.config;

import com.android.sched.util.codec.CodecContext;
import com.android.sched.util.codec.ParsingException;
import com.android.sched.util.config.id.KeyId;
import com.android.sched.util.config.id.ObjectId;
import com.android.sched.util.config.id.PropertyId;
import com.android.sched.util.config.id.PropertyId.Value;
import com.android.sched.util.location.Location;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;

/**
 * Class representing a configuration in order to check it.
 */
public class ConfigChecker {
  @Nonnull
  private final CodecContext context;
  @Nonnull
  private final Map<PropertyId<?>, PropertyId<?>.Value> values =
      new HashMap<PropertyId<?>, PropertyId<?>.Value>();
  @Nonnull
  private final Map<KeyId<?, ?>, Object> instances = new HashMap<KeyId<?, ?>, Object>();
  @Nonnull
  private final Map<KeyId<?, ?>, Location> locations =
      new HashMap<KeyId<?, ?>, Location>();
  @Nonnull
  private final Map<KeyId<?, ?>, String> dropped = new HashMap<KeyId<?, ?>, String>();

  /**
   * @param context Context for parsers
   * @param stringValues All the property values as {@code String} objects.
   * @param instanceValues All the property values as objects.
   */
  ConfigChecker(@Nonnull CodecContext context,
      @Nonnull Map<PropertyId<?>, PropertyId<?>.Value> stringValues,
      @Nonnull Map<ObjectId<?>, Object> instanceValues,
      @Nonnull Map<KeyId<?, ?>, Location> locationsById) {
    this.context = context;
    this.values.putAll(stringValues);
    this.instances.putAll(instanceValues);
    this.locations.putAll(locationsById);
  }

  @Nonnull
  public synchronized <T> T parse(@Nonnull PropertyId<T> propertyId) throws PropertyIdException {
    @SuppressWarnings({"unchecked", "rawtypes"})
    PropertyId<T>.Value value = (PropertyId.Value) values.get(propertyId);

    if (value == null) {
      throw new MissingPropertyException(propertyId);
    }

    try {
      value.check(context);
      return value.getObject(context);
    } catch (ParsingException e) {
      throw new PropertyIdException(propertyId, getLocation(propertyId), e);
    }
  }

  public synchronized <T, S> void check(@Nonnull KeyId<T, S> keyId) throws PropertyIdException {
    if (instances.get(keyId) == null) {
      if (keyId instanceof PropertyId) {
        @SuppressWarnings("unchecked")
        PropertyId<T> propertyId = (PropertyId<T>) keyId;

        @SuppressWarnings({"unchecked", "rawtypes"})
        PropertyId<T>.Value value = (PropertyId.Value) values.get(propertyId);

        if (value == null) {
          throw new MissingPropertyException(propertyId);
        }

        try {
          value.check(context);
        } catch (ParsingException e) {
          throw new PropertyIdException(propertyId, getLocation(propertyId), e);
        }
      } else {
        assert keyId instanceof ObjectId;

        @SuppressWarnings("unchecked")
        ObjectId<T> objectId = (ObjectId<T>) keyId;
        objectId.checkInstantiability();
      }
    }
  }

  @Nonnull
  public <T> String getRawValue(@Nonnull PropertyId<T> propertyId)
      throws MissingPropertyException {
    @SuppressWarnings({"rawtypes", "unchecked"})
    PropertyId<T>.Value value = (Value) values.get(propertyId);

    if (value == null) {
      throw new MissingPropertyException(propertyId);
    }

    return value.getString();
  }

  @Nonnull
  public Map<KeyId<?, ?>, Object> getInstances() {
    return instances;
  }

  @Nonnull
  public Map<PropertyId<?>, PropertyId<?>.Value> getValues() {
    return values;
  }

  @Nonnull
  public Map<KeyId<?, ?>, String> getDropCauses() {
    return dropped;
  }

  @Nonnull
  public Location getLocation(@Nonnull KeyId<?, ?> keyId) {
    assert locations.get(keyId) != null;

    return locations.get(keyId);
  }

  public void remove(@Nonnull KeyId<?, ?> keyId, @Nonnull String cause) {
    values.remove(keyId);
    instances.remove(keyId);
    locations.remove(keyId);
    dropped.put(keyId, cause);
  }
}