update examples · java-tech/codeexamples-java@da140b3 · GitHub
Skip to content

Commit da140b3

Browse files
committed
update examples
1 parent fc84a87 commit da140b3

9 files changed

Lines changed: 101 additions & 43 deletions

File tree

Lines changed: 7 additions & 7 deletions

de.vogella.algorithms.sort.mergesort/src/de/vogella/algorithms/sort/mergesort/Mergesort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.vogella.algorithms.sort.mergesort;
22

3-
43
public class Mergesort {
54
private int[] numbers;
65

@@ -45,11 +44,12 @@ private void merge(int low, int middle, int high) {
4544
while (i <= middle && j <= high) {
4645
if (helper[i] <= helper[j]) {
4746
numbers[k] = helper[i];
47+
i++;
4848
} else {
4949
numbers[k] = helper[j];
50+
j++;
5051
}
5152
k++;
52-
i++;
5353
}
5454
// Copy the rest of the left side of the array into the target array
5555
while (i <= middle) {

de.vogella.algorithms.sort.mergesort/src/de/vogella/algorithms/sort/mergesort/MergesortTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.junit.Before;
1010
import org.junit.Test;
1111

12-
1312
public class MergesortTest {
1413

1514
private int[] numbers;
@@ -25,7 +24,6 @@ public void setUp() throws Exception {
2524
}
2625
}
2726

28-
2927
@Test
3028
public void testMergeSort() {
3129
long startTime = System.currentTimeMillis();
@@ -46,6 +44,25 @@ public void testMergeSort() {
4644

4745
}
4846

47+
@Test
48+
public void itWorksRepeatably() {
49+
for (int i = 0; i < 200; i++) {
50+
numbers = new int[SIZE];
51+
Random generator = new Random();
52+
for (int a = 0; a < numbers.length; a++) {
53+
numbers[a] = generator.nextInt(MAX);
54+
}
55+
Mergesort sorter = new Mergesort();
56+
sorter.sort(numbers);
57+
for (int j = 0; j < numbers.length - 1; j++) {
58+
if (numbers[j] > numbers[j + 1]) {
59+
fail("Should not happen");
60+
}
61+
}
62+
assertTrue(true);
63+
}
64+
}
65+
4966
@Test
5067
public void testStandardSort() {
5168
long startTime = System.currentTimeMillis();
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical"
4-
android:layout_width="fill_parent"
5-
android:layout_height="fill_parent"
6-
>
7-
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Preferences"></Button>
8-
<Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change Preferences" android:onClick="changePreferences"></Button>
3+
android:orientation="vertical" android:layout_width="fill_parent"
4+
android:layout_height="fill_parent">
5+
<EditText android:id="@+id/contextmenu" android:layout_height="wrap_content"
6+
android:text="Context Menu Example" android:layout_width="match_parent"></EditText>
7+
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
8+
android:layout_height="wrap_content" android:text="Show Preferences"></Button>
9+
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
10+
android:layout_height="wrap_content" android:text="Change Preferences"
11+
android:onClick="changePreferences"></Button>
912
</LinearLayout>

de.vogella.android.preferences/src/de/vogella/android/preferences/HelloPreferences.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
import android.content.SharedPreferences.Editor;
77
import android.os.Bundle;
88
import android.preference.PreferenceManager;
9+
import android.view.ContextMenu;
10+
import android.view.ContextMenu.ContextMenuInfo;
911
import android.view.Menu;
1012
import android.view.MenuInflater;
1113
import android.view.MenuItem;
1214
import android.view.View;
1315
import android.view.View.OnClickListener;
1416
import android.widget.Button;
17+
import android.widget.EditText;
1518
import android.widget.Toast;
1619

1720
public class HelloPreferences extends Activity {
21+
private static final int MENU_SECOND = 1;
22+
private static final int MENU_FIRST = 0;
1823
SharedPreferences preferences;
1924

2025
/** Called when the activity is first created. */
2126
@Override
2227
public void onCreate(Bundle savedInstanceState) {
2328
super.onCreate(savedInstanceState);
2429
setContentView(R.layout.main);
30+
31+
EditText text = (EditText) findViewById(R.id.contextmenu);
32+
registerForContextMenu(text);
2533
Button button = (Button) findViewById(R.id.Button01);
2634
// Initialize preferences
2735
preferences = PreferenceManager.getDefaultSharedPreferences(this);
@@ -50,6 +58,11 @@ public void onClick(View v) {
5058
}
5159
edit.putString("username", buffer.toString());
5260
edit.commit();
61+
// A toast is a view containing a quick little message for the
62+
// user. We give a little feedback
63+
Toast.makeText(HelloPreferences.this,
64+
"Reverted string sequence of user name.",
65+
Toast.LENGTH_LONG).show();
5366
}
5467
});
5568
}
@@ -70,7 +83,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
7083
// Launch Preference activity
7184
Intent i = new Intent(HelloPreferences.this, Preferences.class);
7285
startActivity(i);
73-
// A toast is a view containing a quick little message for the user.
86+
// Some feedback to the user
7487
Toast.makeText(HelloPreferences.this,
7588
"Here you can maintain your user credentials.",
7689
Toast.LENGTH_LONG).show();
@@ -79,4 +92,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
7992
}
8093
return true;
8194
}
95+
96+
@Override
97+
public void onCreateContextMenu(ContextMenu menu, View v,
98+
ContextMenuInfo menuInfo) {
99+
// We could make a distinction if several views would have registered a
100+
// context menu. We could also use a XML file but this time we do it
101+
// manually
102+
menu.add(Menu.NONE, MENU_FIRST, Menu.NONE, "Say hello");
103+
menu.add(Menu.NONE, MENU_SECOND, Menu.NONE, "Another option");
104+
}
105+
106+
@Override
107+
public boolean onContextItemSelected(MenuItem item) {
108+
switch (item.getItemId()) {
109+
case MENU_FIRST:
110+
Toast.makeText(this, "First option selected", Toast.LENGTH_SHORT)
111+
.show();
112+
break;
113+
case MENU_SECOND:
114+
Toast.makeText(this, "Second option selected", Toast.LENGTH_SHORT)
115+
.show();
116+
break;
117+
118+
}
119+
return super.onContextItemSelected(item);
120+
}
82121
}

de.vogella.jface.tableviewer/src/de/vogella/jface/tableviewer/View.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected boolean isEditorActivationEvent(
116116
viewer.setContentProvider(new ArrayContentProvider());
117117
// Get the content for the viewer, setInput will call getElements in the
118118
// contentProvider
119-
viewer.setInput(ModelProvider.getInstance().getPersons());
119+
viewer.setInput(ModelProvider.INSTANCE.getPersons());
120120
// Make the selection available to other views
121121
getSite().setSelectionProvider(viewer);
122122
// Set the sorter for the table

de.vogella.jface.tableviewer/src/de/vogella/jface/tableviewer/model/ModelProvider.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,20 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class ModelProvider {
6+
public enum ModelProvider {
7+
INSTANCE;
78

8-
private static ModelProvider content;
99
private List<Person> persons;
1010

1111
private ModelProvider() {
1212
persons = new ArrayList<Person>();
1313
// Image here some fancy database access to read the persons and to
1414
// put them into the model
15-
Person person;
16-
person = new Person("Rainer", "Zufall", "male", true);
17-
persons.add(person);
18-
person = new Person("Reiner", "Babbel", "male", true);
19-
persons.add(person);
20-
person = new Person("Marie", "Dortmund", "female", false);
21-
persons.add(person);
22-
person = new Person("Holger", "Adams", "male", true);
23-
persons.add(person);
24-
person = new Person("Juliane", "Adams", "female", true);
25-
persons.add(person);
26-
27-
}
28-
29-
public static synchronized ModelProvider getInstance() {
30-
if (content != null) {
31-
return content;
32-
}
33-
content = new ModelProvider();
34-
return content;
15+
persons.add(new Person("Rainer", "Zufall", "male", true));
16+
persons.add(new Person("Reiner", "Babbel", "male", true));
17+
persons.add(new Person("Marie", "Dortmund", "female", false));
18+
persons.add(new Person("Holger", "Adams", "male", true));
19+
persons.add(new Person("Juliane", "Adams", "female", true));
3520
}
3621

3722
public List<Person> getPersons() {

de.vogella.junit.first/test/de/vogella/junit/first/MyClassTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package de.vogella.junit.first;
22

3-
import org.junit.Test;
4-
53
import static org.junit.Assert.assertEquals;
64

5+
import org.junit.Test;
6+
77
public class MyClassTest {
88

99
@Test
Lines changed: 14 additions & 0 deletions

0 commit comments

Comments
 (0)