Table of Contents Previous Next
Logo
Client-Side Slice-to-Java Mapping : 10.7 Mapping for User-Defined Types
Copyright © 2003-2010 ZeroC, Inc.

10.7 Mapping for User-Defined Types

Slice supports user-defined types: enumerations, structures, sequences, and dictio­naries.

10.7.1 Mapping for Enumerations

A Slice enum type maps to the Java enum type. Consider the following example:
enum Fruit { Apple, Pear, Orange };
The Java mapping for Fruit is shown below:
public enum Fruit implements java.io.Serializable {
    Apple,
    Pear,
    Orange;

    // ...
}
Given the above definitions, we can use enumerated values as follows:
Fruit favoriteFruit = Fruit.Apple;
Fruit otherFavoriteFruit = Fruit.Orange;

if (favoriteFruit == Fruit.Apple) // Compare with constant
    // ...

if (f1 == f2)                     // Compare two enums
    // ...

switch (f2) {             // Switch on enum
case Fruit.Apple:
    // ...
    break;
case Fruit.Pear
    // ...
    break;
case Fruit.Orange
    // ...
    break;
}
Note that the generated class contains a number of other members, which we have not shown. These members are internal to the Ice run time and you must not use them in your application code (because they may change from release to release).

10.7.2 Mapping for Structures

Slice structures map to Java classes with the same name. For each Slice data member, the Java class contains a corresponding public data member. For example, here is our Employee structure from Section 4.9.4 once more:
struct Employee {
    long number;
    string firstName;
    string lastName;
};
The Slice-to-Java compiler generates the following definition for this structure:
public final class Employee implements java.lang.Cloneable,
                                       java.io.Serializable {
    public long number;
    public String firstName;
    public String lastName;

    public Employee {}

    public Employee(long number,
                    String firstName,
                    String lastName) {
        this.number = number;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public boolean equals(java.lang.Object rhs) {
        // ...
    }
    public int hashCode() {
        // ...
    }

    public java.lang.Object clone()
        java.lang.Object o;
        try
        {
            o = super.clone();
        }
        catch(java.lang.CloneNotSupportedException ex)
        {
            assert false; // impossible
        }
        return o;
    }
}
For each data member in the Slice definition, the Java class contains a corre­sponding public data member of the same name. Refer to Section 10.16.3 for additional information on data members.
The equals member function compares two structures for equality. Note that the generated class also provides the usual hashCode and clone methods. (clone has the default behavior of making a shallow copy.)

Constructors

Structures have a default constructor that default-constructs each data member. This means members of primitive type are initialized to the equivalent of zero, and members of reference type are initialized to null. Note that applications must always explicitly initialize members of structure and enumerated types because the Ice run time does not accept null as a legal value for these types.
If you wish to ensure that data members of primitive and enumerated types are initialized to specific values, you can declare default values in your Slice defini­tion (see Section 4.9.2). The default constructor initializes each of these data members to its declared value.
Structures also have a second constructor that has one parameter for each data member. This allows you to construct and initialize a class instance in a single statement (instead of first having to construct the instance and then assigning to its members).

10.7.3 Mapping for Sequences

Slice sequences map to Java arrays. This means that the Slice-to-Java compiler does not generate a separate named type for a Slice sequence. For example:
sequence<Fruit> FruitPlatter;
This definition simply corresponds to the Java type Fruit[]. Naturally, because Slice sequences are mapped to Java arrays, you can take advantage of all the array functionality provided by Java, such as initialization, assignment, cloning, and the length member. For example:
Fruit[] platter = { Fruit.Apple, Fruit.Pear };
assert(platter.length == 2);
See Section 10.16 for information on alternate mappings for sequence types.

10.7.4 Mapping for Dictionaries

Here is the definition of our EmployeeMap from Section 4.9.4 once more:
dictionary<long, Employee> EmployeeMap;
As for sequences, the Java mapping does not create a separate named type for this definition. Instead, the dictionary is simply an instance of the generic type java.util.Map<K, V>, where K is the mapping of the key type and V is the mapping of the value type. In the example above, EmployeeMap is mapped to the Java type java.util.Map<Long, Employee>. The following code demon­strates how to allocate and use an instance of EmployeeMap:
java.util.Map<Long, Employee> em =
    new java.util.HashMap<Long, Employee>();

Employee e = new Employee();
e.number = 31;
e.firstName = "James";
e.lastName = "Gosling";

em.put(e.number, e);
The typesafe nature of the mapping makes iterating over the dictionary quite convenient:
for (java.util.Map.Entry<Long, Employee> i : em.entrySet()) {
    long num = i.getKey();
    Employee emp = i.getValue();
    System.out.println(emp.firstName + " was employee #" + num);
}
See Section 10.16 for information on alternate mappings for dictionary types.

Table of Contents Previous Next
Logo