Getting Started with Sugar in Oxygene for Java

See a clear introduction to Sugar on the RemObjects website. In short, the Sugar library enables you to share some of your code between the different platforms that you are targeting. You can see below code for creating and using a dictionary in OxygeneJavaSugarDemo. You need to add a reference to Sugar. Compare this with the very similar code in OxygeneDotNetSugarDemo in Getting Started with Oxygene .Net.

namespace OxygeneJavaSugarDemo;

interface

uses
  java.util, sugar.collections;

type
  ConsoleApp = class
  private
    class var capitals : sugar.collections.Dictionary<String, String>;
    class var inputBytes: array[0..30] of SByte;
    class var country: java.lang.String;
    class var inputLength: Integer;
  public
    class method Main(args: array of String);
    class method MakeDictionary;
  end;

implementation

class method ConsoleApp.MakeDictionary;
begin
  capitals := new sugar.collections.Dictionary<String, String>;
  capitals.Add("England", "London");
  capitals.Add("Scotland", "Edinburgh");
  capitals.Add("Northern Ireland", "Belfast");
  capitals.Add("Wales", "Cardiff");
  capitals.Add("Ireland", "Dublin");
  capitals.Add("Germany", "Berlin");
  capitals.Add("France", "Paris");
  capitals.Add("Greece", "Athens");
  capitals.Add("Finland", "Helsinki");
  capitals.Add("Hungary", "Budapest");
  capitals.Add("Italy", "Rome");
  capitals.Add("Liechtenstein", "Vaduz");
  capitals.Add("Lithuania", "Vilnius");
  capitals.Add("Luxembourg", "Luxembourg");
  capitals.Add("Poland", "Warsaw");
  capitals.Add("Portugal", "Lisbon");
  capitals.Add("Romania", "Bucharest");
  capitals.Add("Russia", "Moscow");
  capitals.Add("Serbia", "Belgrade");
  capitals.Add("Slovakia", "Bratislava");
  capitals.Add("Slovenia", "Ljubljana");
  capitals.Add("Spain", "Madrid");
  capitals.Add("Sweden", "Stockholm");
  capitals.Add("Switzerland", "Bern");
  capitals.Add("Norway", "Oslo");
  capitals.Add("Austria", "Vienna");
  capitals.Add("Belgium", "Brussels");
  capitals.Add("Bosnia and  Herzegovina", "Sarajevo");
  capitals.Add("Bulgaria", "Sofia");
  capitals.Add("Netherlands", "Amsterdam");
  capitals.Add("Czech Republic", "Prague");
  capitals.Add("Moldova", "Chisinau");
  capitals.Add("Latvia", "Riga");
  capitals.Add("Iceland", "Reykjavik");
  capitals.Add("Malta", "Valletta");
  capitals.Add("Macedonia", "Skopje");
  capitals.Add("Montenegro", "Podgorica");
  capitals.Add("Turkey", "Ankara");
  capitals.Add("Estonia", "Tallinn");
end;

class method ConsoleApp.Main(args: array of String);  
begin
  MakeDictionary;
  System.out.println('Type end to finish.');
  repeat
    System.out.print('Please enter a European country to find its capital: ');
    inputLength := System.in.read(inputBytes);
    country := new  java.lang.String(inputBytes, 0, inputLength - System.lineSeparator.length);
    if country = 'end' then
      exit;
    try
      System.out.println('Capital of ' + country + ': ' + capitals[country]);
      System.out.println();
    except
      System.out.println(country + "not found");
    end;
  until country = 'end'; 
end;

end.

Equivalent Code in RemObjects C#

To compile this demo you need to add a reference to sugar.jar.

using java.util;
using sugar;

namespace sugar_demo_cs_java
{
  static class Program
  { private 
      SByte[] inputBytes = new SByte[30];
      string country;
      Integer inputLength; 
      sugar.collections.Dictionary<String, String> capitals;

    public static void Main(string[] args)
    {
      capitals = new sugar.collections.Dictionary<String, String>()
      {
        {"England", "London"},
        {"Scotland", "Edinburgh"},
        {"Northern Ireland", "Belfast"},
        {"Wales", "Cardiff"},
        {"Ireland", "Dublin"},
        {"Germany", "Berlin"},
        {"France", "Paris"},
        {"Greece", "Athens"},
        {"Finland", "Helsinki"},
        {"Hungary", "Budapest"},
        {"Italy", "Rome"},
        {"Liechtenstein", "Vaduz"},
        {"Lithuania", "Vilnius"},
        {"Luxembourg", "Luxembourg"},
        {"Poland", "Warsaw"},
        {"Portugal", "Lisbon"},
        {"Romania", "Bucharest"},
        {"Russia", "Moscow"},
        {"Serbia", "Belgrade"},
        {"Slovakia", "Bratislava"},
        {"Slovenia", "Ljubljana"},
        {"Spain", "Madrid"},
        {"Sweden", "Stockholm"},
        {"Switzerland", "Bern"},
        {"Norway", "Oslo"},
        {"Austria", "Vienna"},
        {"Belgium", "Brussels"},
        {"Bosnia and Herzegovina", "Sarajevo"},
        {"Bulgaria", "Sofia"},
        {"Netherlands", "Amsterdam"},
        {"Czech Republic", "Prague"},
        {"Moldova", "Chisinau"},
        {"Latvia", "Riga"},
        {"Iceland", "Reykjavik"},
        {"Malta", "Valletta"},
        {"Macedonia", "Skopje"},
        {"Montenegro", "Podgorica"},
        {"Turkey", "Ankara"}
      };
      capitals.Add("Estonia", "Tallinn");
      System.@out.println("Please enter a European country to find its capital: ");
      inputLength = System.@in.read(inputBytes);
      country = new string(inputBytes, 0, inputLength - System.lineSeparator().length());
      try
      {
        System.@out.println("Capital of " + country + ": " + capitals[country]);
      }
      catch(Exception E)
      {
        System.@out.println(country + " not found");
      };
      System.@in.read();
    }
  }
}


Programming - a skill for life!

Language features such as loops, generics, class contracts, asynchronous methods, futures and Sugar in Oxygene for Java