View Javadoc

1   /*
2      Copyright 2010 Ramon Servadei
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  package mirrormap.commands;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.ObjectOutputStream;
20  import java.io.Serializable;
21  
22  import mirrormap.MirrorMap;
23  import mirrormap.Utils;
24  
25  /**
26   * Base class for {@link ICommand} implementations
27   * 
28   * @author Ramon Servadei
29   */
30  public abstract class AbstractCommand implements ICommand
31  {
32      private static final long serialVersionUID = 1L;
33  
34      /** The name of the {@link MirrorMap} instance this command applies to */
35      protected String mirrorMapName;
36  
37      /** The key in the {@link MirrorMap} the command applies to */
38      protected Serializable key;
39  
40      public AbstractCommand()
41      {
42          super();
43      }
44  
45      public byte[] getBytes()
46      {
47          try
48          {
49              ByteArrayOutputStream bos = new ByteArrayOutputStream();
50              ObjectOutputStream oos = new ObjectOutputStream(bos);
51              oos.writeObject(this);
52              oos.close();
53              return bos.toByteArray();
54          }
55          catch (Exception e)
56          {
57              throw new RuntimeException("Could not serialise "
58                  + Utils.safeToString(this), e);
59          }
60      }
61  
62      public String getMirrorMapName()
63      {
64          return mirrorMapName;
65      }
66  
67      @Override
68      public String toString()
69      {
70          return Utils.string(this, mirrorMapName + ", " + key);
71      }
72  }