View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model.state;
17  
18  import org.fourthline.cling.model.Command;
19  import org.fourthline.cling.model.ServiceManager;
20  import org.fourthline.cling.model.meta.LocalService;
21  import org.fourthline.cling.model.meta.StateVariable;
22  
23  
24  /**
25   * Reads the value of a state variable, given an instance that implements the service.
26   *
27   * TODO: The design of this is not final, not happy with the relationship between ActionExecutor and this.
28   *
29   * @author Christian Bauer
30   */
31  public abstract class StateVariableAccessor {
32  
33      public StateVariableValue read(final StateVariable<LocalService> stateVariable, final Object serviceImpl) throws Exception {
34  
35          class AccessCommand implements Command {
36              Object result;
37              public void execute(ServiceManager serviceManager) throws Exception {
38                  result = read(serviceImpl);
39                  if (stateVariable.getService().isStringConvertibleType(result)) {
40                      result = result.toString();
41                  }
42              }
43          }
44  
45          AccessCommand cmd = new AccessCommand();
46          stateVariable.getService().getManager().execute(cmd);
47          return new StateVariableValue(stateVariable, cmd.result);
48      }
49  
50      public abstract Class<?> getReturnType();
51  
52      // TODO: Especially this shouldn't be public
53      public abstract Object read(Object serviceImpl) throws Exception;
54  
55      @Override
56      public String toString() {
57          return "(" + getClass().getSimpleName() + ")";
58      }
59  }