examples: annotate examples
[nit.git] / contrib / jwrapper / examples / queue / Queue.java
1 /*
2 * This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */
16
17 import java.util.*;
18
19 public class Queue
20 {
21 // function pointer
22 public native void printError( String errorMsg );
23
24 // internal list
25 private LinkedList<String> list;
26
27 public Queue()
28 {
29 list = new LinkedList<String>();
30 }
31
32 public void push( String element )
33 {
34 System.out.print( "From java, pushing " );
35 System.out.print( element );
36 System.out.print( "\n" );
37 list.addLast( element );
38 }
39
40 public String pop() // knows where is native printError
41 {
42 String element;
43
44 try
45 {
46 element = list.removeFirst();
47 }
48 catch ( NoSuchElementException e )
49 {
50 printError( "From java, empty queue." );
51 element = null;
52 throw e;
53 }
54
55 System.out.print( "From java, popping " );
56 System.out.print( element );
57 System.out.print( "\n" );
58
59 return element;
60 }
61 }