Home | History | Annotate | Download | only in stateful
      1 /*
      2  * Copyright (C) 2015 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 package com.android.camera.captureintent.stateful;
     18 
     19 import com.google.common.base.Optional;
     20 
     21 import java.util.HashMap;
     22 import java.util.Map;
     23 
     24 import javax.annotation.ParametersAreNonnullByDefault;
     25 
     26 /**
     27  * Provides a basic implementation for {@link State} interface.
     28  */
     29 @ParametersAreNonnullByDefault
     30 public abstract class StateImpl implements State {
     31     private final StateMachine mStateMachine;
     32     private final Map<Class, EventHandler> mEventHandlerMap;
     33 
     34     protected StateImpl(StateMachine stateMachine) {
     35         mStateMachine = stateMachine;
     36         mEventHandlerMap = new HashMap<>();
     37     }
     38 
     39     protected StateImpl(State previousState) {
     40         this(previousState.getStateMachine());
     41     }
     42 
     43     @Override
     44     public StateMachine getStateMachine() {
     45         return mStateMachine;
     46     }
     47 
     48     @Override
     49     public Optional<State> onEnter() {
     50         return NO_CHANGE;
     51     }
     52 
     53     @Override
     54     public void onLeave() {
     55     }
     56 
     57     @Override
     58     public int getEventHandlerCount() {
     59         return mEventHandlerMap.size();
     60     }
     61 
     62     @Override
     63     public final <T extends Event> EventHandler<T> getEventHandler(Class<T> eventClass) {
     64         return mEventHandlerMap.get(eventClass);
     65     }
     66 
     67     @Override
     68     public final <T extends Event> void setEventHandler(
     69             Class<T> eventClass, EventHandler<T> eventHandler) {
     70         mEventHandlerMap.put(eventClass, eventHandler);
     71     }
     72 
     73     @Override
     74     public <T extends Event> void removeEventHandler(Class<T> eventClass) {
     75         mEventHandlerMap.remove(eventClass);
     76     }
     77 }