View Javadoc
1   /*
2    * Copyright 2014 Jin Kwon <jinahya_at_gmail.com>.
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  
18  package com.github.jinahya.simple.file.back;
19  
20  
21  import com.github.jinahya.simple.file.back.FileContext.PropertyKey;
22  import java.util.EnumMap;
23  import java.util.Map;
24  import java.util.Optional;
25  
26  
27  /**
28   * An abstract class partially implementing {@link FileContext}.
29   *
30   * @author Jin Kwon <jinahya_at_gmail.com>
31   */
32  public abstract class AbstractFileContext implements FileContext {
33  
34  
35      @Override
36      public Optional<Object> property(final PropertyKey key) {
37  
38          if (key == null) {
39              throw new NullPointerException("null key");
40          }
41  
42          return Optional.ofNullable(properties().get(key));
43      }
44  
45  
46      @Override
47      public Optional<Object> property(final PropertyKey key,
48                                       final Object value) {
49  
50          if (key == null) {
51              throw new NullPointerException("null key");
52          }
53  
54          if (value == null) {
55              return Optional.ofNullable(properties().remove(key));
56          }
57  
58          return Optional.ofNullable(properties().put(key, value));
59      }
60  
61  
62      /**
63       * Returns a map of properties.
64       *
65       * @return properties.
66       */
67      protected Map<PropertyKey, Object> properties() {
68  
69          if (properties == null) {
70              properties = new EnumMap<>(PropertyKey.class);
71          }
72  
73          return properties;
74      }
75  
76  
77      private Map<PropertyKey, Object> properties;
78  
79  
80  }
81