Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 86a19be097f5b9aad81c9c95825b5f9ebb32ac42 (plain) (blame)
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
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.transfer;

import org.eclipse.net4j.util.ObjectUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Eike Stepper
 * @since 4.2
 */
public class CDOTransferType implements Comparable<CDOTransferType>
{
  /**
   * Must be declared/initialized <b>before</b> any transfer types!
   */
  private static final Map<String, CDOTransferType> MAP = new HashMap<String, CDOTransferType>();

  public static final CDOTransferType UNKNOWN = new CDOTransferType()
  {
    @Override
    public String toString()
    {
      return "<Unknown>";
    }
  };

  public static final CDOTransferType FOLDER = new CDOTransferType()
  {
    @Override
    public String toString()
    {
      return "<Folder>";
    }
  };

  public static final CDOTransferType MODEL = new CDOTransferType()
  {
    @Override
    public String toString()
    {
      return "<Model>";
    }
  };

  public static final CDOTransferType BINARY = new CDOTransferType()
  {
    @Override
    public String toString()
    {
      return "<Binary>";
    }
  };

  public static final Text UTF8 = text("UTF-8");

  public static final Set<CDOTransferType> STANDARD_TYPES = new HashSet<CDOTransferType>(Arrays.asList(UNKNOWN, FOLDER,
      MODEL, BINARY, UTF8));

  public static final Map<String, CDOTransferType> REGISTRY = Collections.unmodifiableMap(MAP);

  private CDOTransferType()
  {
    this(true);
  }

  private CDOTransferType(boolean register)
  {
    if (register)
    {
      MAP.put(toString(), this);
    }
  }

  @Override
  public int hashCode()
  {
    return toString().hashCode();
  }

  @Override
  public boolean equals(Object obj)
  {
    return ObjectUtil.equals(toString(), obj.toString());
  }

  public int compareTo(CDOTransferType o)
  {
    return toString().compareTo(o.toString());
  }

  public static Text text(String encoding)
  {
    CDOTransferType type = MAP.get(encoding);
    if (type instanceof Text)
    {
      return (Text)type;
    }
  
    if (type == null)
    {
      Text text = new Text(encoding);
      MAP.put(encoding, text);
      return text;
    }
  
    throw new IllegalArgumentException("Illegal encoding: " + encoding);
  }

  /**
   * @author Eike Stepper
   */
  public static class Text extends CDOTransferType
  {
    private String encoding;

    private Text(String encoding)
    {
      super(false);
      this.encoding = encoding;
    }

    public String getEncoding()
    {
      return encoding;
    }

    @Override
    public String toString()
    {
      return encoding;
    }
  }
}

Back to the top