1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

For sorter example

This commit is contained in:
Kevin Glynn 2011-07-20 11:39:39 +02:00
parent 882da155ca
commit 56eac9193a
7 changed files with 166 additions and 4 deletions

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
-->
<Interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:www.twigletsoftware.com:schemas:txtemplate:1:0">
<Imports>
<Import>java.util.Comparator</Import>
</Imports>
<Java>Comparator*[${T}]*</Java>
<Name>System.Collections.Generic.IComparer</Name>
<TypeParams>
<Name>T</Name>
</TypeParams>
<Methods>
<Method>
<Params>
<Param>
<Type>T</Type>
<Name>x</Name>
</Param>
<Param>
<Type>T</Type>
<Name>y</Name>
</Param>
</Params>
<Java>compare(${x}, ${y})</Java>
<Name>Compare</Name>
</Method>
</Methods>
<Properties>
</Properties>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /><DigestValue>FzD0ISaWZKb/GDgZSI4werJrHHs=</DigestValue></Reference></SignedInfo><SignatureValue>NeU9k5KOXySVLGMadMEZfh5bsC928iltYWSnpm/5N0sN3NSyfZYUPZ3mL8DG8bZPzJbTIla2fkkZH0FbsDbtU1AeTYpFg9C8NSGDeYqR1mNEaCqYUZQmmP5MP9TtyT9dZOItpb4TXSWjKdkahUaw6CbFRn38Az9Wfh+8DdHIzjg=</SignatureValue></Signature></Interface>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is
@ -44,6 +44,17 @@
<Name>ToString</Name>
<Return>System.String</Return>
</Method>
<Method>
<Java>${value} == ${this} ? 0 : (${value} > ${this} ? 1 : -1)</Java>
<Params>
<Param>
<Type>System.Number</Type>
<Name>value</Name>
</Param>
</Params>
<Name>CompareTo</Name>
<Return>System.Int32</Return>
</Method>
</Methods>
<Properties />
<Events />

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is
@ -20,6 +20,7 @@
<Java>${expr}.toCharArray()</Java>
</Iterable>
<Methods>
<!-- see http://msdn.microsoft.com/en-us/library/dd465121.aspx for string comparison details -->
<Method>
<Imports>
<Import>CS2JNet.System.StringSupport</Import>
@ -38,6 +39,20 @@
<Name>Compare</Name>
<Return>System.Int32</Return>
</Method>
<Method>
<Imports>
<Import>CS2JNet.System.StringSupport</Import>
</Imports>
<Java>StringSupport.Compare(${arg1}, ${this})</Java>
<Params>
<Param>
<Type>System.String</Type>
<Name>arg1</Name>
</Param>
</Params>
<Name>CompareTo</Name>
<Return>System.Int32</Return>
</Method>
<Method>
<Imports>
<Import>CS2JNet.System.StringSupport</Import>

View File

@ -80,7 +80,7 @@ public class StringSupport {
public static int Compare(String s1, String s2, boolean ignoreCase)
{
// Handele nulls, null is smaller than a real string
// Handle nulls, null is smaller than a real string
if (s1 == null) {
if (s2 == null) {
return 0;

View File

@ -14,7 +14,8 @@ namespace Tester
//Tester.Delegates.EventHandler.Sample.EHMain();
Tester.Misc.RenameTest.RNMain("hello world");
// Tester.Misc.RenameTest.RNMain("hello world");
Tester.Misc.Sorter.Example.SorterMain();
}
}

View File

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
namespace Tester.Misc.Sorter
{
public class Example : IComparer<string>
{
private static int CompareDinosByLength (string x, string y)
{
if (x == null) {
if (y == null) {
// If x is null and y is null, they're
// equal.
return 0;
} else {
// If x is null and y is not null, y
// is greater.
return -1;
}
} else {
// If x is not null...
//
// ...and y is null, x is greater.
if (y == null) {
return 1;
} else {
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo (y.Length);
if (retval != 0) {
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
} else {
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo (y);
}
}
}
}
public int Compare(String x, String y) {
return CompareDinosByLength(x,y);
}
public static void SorterMain ()
{
List<string> dinosaurs = new List<string> ();
dinosaurs.Add ("Pachycephalosaurus");
dinosaurs.Add ("Amargasaurus");
dinosaurs.Add ("");
dinosaurs.Add (null);
dinosaurs.Add ("Mamenchisaurus");
dinosaurs.Add ("Deinonychus");
Display (dinosaurs);
Console.WriteLine ("\nSort with generic Comparison<string> delegate:");
dinosaurs.Sort (new Example());
Display (dinosaurs);
}
private static void Display (List<string> list)
{
Console.WriteLine ();
foreach (string s in list) {
if (s == null)
Console.WriteLine ("(null)");
else
Console.WriteLine ("\"{0}\"", s);
}
}
}
}
/* This code example produces the following output:
"Pachycephalosaurus"
"Amargasaurus"
""
(null)
"Mamenchisaurus"
"Deinonychus"
Sort with generic Comparison<string> delegate:
(null)
""
"Deinonychus"
"Amargasaurus"
"Mamenchisaurus"
"Pachycephalosaurus"
*/

View File

@ -58,6 +58,7 @@
<Compile Include="Delegates\EventHandler.cs" />
<Compile Include="Misc\RenameTest.cs" />
<Compile Include="Misc\newmodifier.cs" />
<Compile Include="Misc\Sorter.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>