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

10.7 Mapping for User-Defined Types

Slice supports user-defined types: enumerations, structures, sequences, and dictionaries.

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 {
    Apple(0),
    Pear(1),
    Orange(2);

    public static final int _Apple = 0;
    public static final int _Pear = 1;
    public static final int _Orange = 2;

    public int
    value() {
        // ...
    }

    public static Fruit
    convert(int val) {
        // ...
    }

    public static Fruit
    convert(String val) {
        // ...
    }

    // ...
}
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;
}
The value and convert methods act as an accessor and a modifier, so you can read and write the value of an enumerated variable as an integer. If you are using the convert method, you must make sure that the passed value is within the range of the enumeration; failure to do so will result in an assertion failure:
Fruit favoriteFruit = Fruit.convert(4); // Assertion failure!
The static members such as _Apple that supply the integer values of each enumerator are retained for backward compatibility with the Java2 mapping.
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).
See Section 10.15.2 for information on the Java2 mapping for Slice enumerations.

10.7.2 Mapping for Structures

Slice structures map to Java structures 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 {
    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 corresponding public data member of the same name. Refer to Section 10.15.4 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.)
The Java class also has a default constructor as well as a second constructor that accepts one argument for each data member of the structure. This constructor allows you to construct and initialize a structure in a single statement (instead of having to first instantiate the structure and then initialize 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.15 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 demonstrates 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.15 for information on alternate mappings for dictionary types.
Table of Contents Previous Next
Logo