Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 10f70503c05d04e8dcf44d680e4735e1519e1af0 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2012 Eike Stepper (Loehne, 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.emf.cdo.internal.transfer.bundle.OM;

import org.eclipse.net4j.util.ObjectUtil;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Specifies how to treat a source {@link CDOTransferElement element} when it is mapped to a target element by a {@link CDOTransferMapping transfer mapping}.
 *
 * @author Eike Stepper
 * @since 4.2
 * @noextend This class is not intended to be subclassed by clients.
 */
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 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 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);
  }

  static
  {
    try
    {
      for (Charset charset : Charset.availableCharsets().values())
      {
        try
        {
          text(charset.toString());
        }
        catch (Exception ex)
        {
          OM.LOG.error(ex);
        }
      }
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
    }
  }

  /**
   * A {@link CDOTransferType transfer type} for text {@link CDOTransferElement elements} that have a special {@link #getEncoding() encoding}.
   * <p>
   * Call {@link CDOTransferType#text(String)} to get an encoding-specific instance.
   *
   * @author Eike Stepper
   */
  public static final 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