Do not use wildcard types as return types.
E.g.,
public static <E> Set<E> union(Set<E> s1, Set<E> s2)should be re-written
public static <E> Set<E> union(Set<? extends E> s1, Set<? extends E> s2)
public static <E> Set<E> union(Set<E> s1, Set<E> s2)should be re-written
public static <E> Set<E> union(Set<? extends E> s1, Set<? extends E> s2)
// pushAll method without wildcard type - deficient! public void pushAll(Iterable<E> src) { for (E e : src) { push(e); } }should be re-written:
// Wildcard type for parameter that serves as an E producer public void pushAll(Iterable<? extends E> src) { for (E e : src) { push(e); } }Similarly, a method that pops each element off the stack and adds the elements to the given collection:
// popAll method without wildcard type - deficient! public void popAll(Collection<E> dst) { while (!isEmpty()) { dst.add(pop()); } }should be re-written:
// Wildcard type for parameter that serves as an E consumer public void popAll(Collection<? super E> dst) { while (!isEmpty()) { dst.add(pop()); } }
public static void swap(List<?> list, int i, int j);over
public static <E> void swap(List<E> list, int i, int j);However, this implementation won't compile:
public static void swap(List<?> list, int i, int j) { list.set(i, list.set(j, list.get(i))); }because you can’t put any value except null into a List<?>. Instead, we will have to use:
public static void swap(List<?> list, int i, int j) { swapHelper(list, i, j); } // Private helper method for wildcard capture private static <E> void swapHelper(List<E> list, int i, int j) { list.set(i, list.set(j, list.get(i))); }
// Wrapper class - uses composition in place of inheritance public class InstrumentedSet<E> extends ForwardingSet<E> { private int addCount = 0; public InstrumentedSet(Set<E> s) { super(s); } @Override public boolean add(E e) { addCount++; return super.add(e); } @Override public boolean addAll(Collection<? extends E> c) { addCount += c.size(); return super.addAll(c); } public int getAddCount() { return addCount; } }
// Reusable forwarding class public class ForwardingSet<E> implements Set<E> { private final Set<E> s; public ForwardingSet(Set<E> s) { this.s = s; } public void clear() { s.clear(); } public boolean contains(Object o) { return s.contains(o); } public boolean isEmpty() { return s.isEmpty(); } public int size() { return s.size(); } public Iterator<E> iterator() { return s.iterator(); } public boolean add(E e) { return s.add(e); } public boolean remove(Object o) { return s.remove(o); } public boolean containsAll(Collection<?> c) { return s.containsAll(c); } public boolean addAll(Collection<? extends E> c) { return s.addAll(c); } public boolean removeAll(Collection<?> c) { return s.removeAll(c); } public boolean retainAll(Collection<?> c) { return s.retainAll(c); } public Object[] toArray() { return s.toArray(); } public <T> T[] toArray(T[] a) { return s.toArray(a); } @Override public boolean equals(Object o) { return s.equals(o); } @Override public int hashCode() { return s.hashCode(); } @Override public String toString() { return s.toString(); } }
// Example use Set<Date> s = new InstrumentedSet<Date>(new TreeSet<Date>(comparator));
// Potential security hole! public static final Thing[] VALUES = { ... };Solution:
private static final Thing[] PRIVATE_VALUES = { ... }; public static final List<Thing> VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));Or:
private static final Thing[] PRIVATE_VALUES = { ... }; public static final Thing[] values() { return PRIVATE_VALUES.clone(); }
public abstract class AbstractShape { private DrawingProgram dp; public abstract void draw(); public AbstractShape(DrawingProgram dp) { this.dp = dp; } public void drawLine() { this.dp.drawLine(); } public void drawCircle() { this.dp.drawCircle(); } }
public class Rectangle extends AbstractShape { public Rectangle(DrawingProgram dp) { super(dp); } @Override public void draw() { drawLine(); drawLine(); drawLine(); drawLine(); } }
public class Circle extends AbstractShape { public Circle(DrawingProgram dp) { super(dp); } @Override public void draw() { drawCircle(); } }
public interface DrawingProgram { void drawLine(); // implicitly public void drawCircle(); }
public class DrawingProgram1 implements DrawingProgram { @Override public void drawLine() { ... } @Override public void drawCircle() { ... } }
public class DrawingProgram2 implements DrawingProgram { @Override public void drawLine() { ... } @Override public void drawCircle() { ... } }
public class client { public static void main(String[] args) { Drawing dp1 = new DrawingProgram1(); Rectangle rectangle = new Rectangle(dp1); Drawing dp2 = new DrawingProgram2(); Circle circle = new Circle(dp2); circle.draw(); rectangle.draw(); } }
public interface WindowFactory { Window createWindow(); // implicitly public }
public MsWindowFactory implements WindowFactory { @Override public Window createWindow() { return new MsWindow(); } }
public AppleWindowFactory implements WindowFactory { @Override public Window createWindow() { return new AppleWindow(); } }
public Client { public static void main(String[] args) { WindowFactory factory; String osName = System.getProperty("os.name"); if ((osName != null) && (osName.indexOf("Windows") != -1)) { factory = new MsWindowFactory(); } else { factory = new AppleWindowFactory(); } Window window = factory.createWindow(); } }
private Document doc; ... JButton button = new JButton("Load"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { SwingWorker<Document, Void> worker = new SwingWorker<Document, Void>() { @Override public Document doInBackground() { return load(); } @Override public void done() { try { doc = get(); } catch (Exception e) { e.printStackTrace(); } } }; worker.execute(); } });
SwingUtilities.invokeLater(Runnable):
SwingUtilities.invokeAndWait(Runnable):
sudo mount /dev/cdrom /media/cdrom
cd /media/cdrom
sudo ./VBoxLinuxAdditions.run
sudo reboot
public interface Logger { void debug(String message); // implicitly public }
public class FileLogger implements Logger { @Override public void debug(String message) { ... } }
public abstract class AbstractLoggerFactory { public abstract Logger createLogger(); // factory method public Logger getLogger() { Logger logger = createLogger(); ... return logger; } }
public class FileLoggerFactory extends AbstractLoggerFactory { @Override public Logger createLogger() { return new FileLogger(); } }
public class Client { private AbstractLoggerFactory factory = new FileLoggerFactory(); public doSomething(AbstractLoggerFactory factory) { Logger logger = factory.getLogger(); logger.debug("debug message"); } }
// Enum singleton - the preferred approach: concise + serializable public enum Singleton { // implicitly final INSTANCE; // implicitly public static final public void doSomething() { ... } }
public interface Visitable { void accept(Visitor visitor); // implicitly public }
public class Book implements Visitable { @Override public void accept(Visitor visitor) { if (...) visitor.visit(this); } }
public interface Visitor { void visit(Book book); // implicitly public void visit(CD cd); }
public class PostageVisitor implements Visitor { @Override public void visit(Book book) { this.totalPostage += ...; } @Override public void visit(CD cd) { this.totalPostage += ...; } public void getTotalPostage() { return this.totalPostage; } }
public class Client { public double calculatePostage() { PostageVisitor visitor = new PostageVisitor(); for (Visitable item : this.items) { item.accept(visitor); } return visitor.getTotalPostage(); } }
code { ... display: block; }
.syntaxhighlighter { padding: 1px !important; }
tar czf - 3rdparty common ectasea extensions global.inc index.php login
| ssh me@mamlan.com "tar xzf - -C www/ectasea"
MyJFrame.this.processWindowEvent( new WindowEvent(MyJFrame.this,WindowEvent.WINDOW_CLOSED));
AWT.EventQueueClass=jnewmarch.replay.TracedEventQueue
to this, then all events that enter the event queue will be printed to standard output.
public class Person { private String name; private Date birth; public boolean equals(Object other) { if (other == this) return true; if (other == null) return false; if (getClass() != other.getClass()) return false; Person person = (Person)other; return ( (name == person.name || (name != null && name.equals(person.name))) && (birth == person.birth || (birth != null && birth.equals(person.birth))) ); } }
static Frame JOptionPane.getFrameForComponent(Component)
// Always pack before using setLocationRelativeTo
myDialog.dialog.pack();
myDialog.dialog.setLocationRelativeTo(frame);
boolean rowIsAnchor = (getSelectionModel().getAnchorSelectionIndex() == row);
boolean colIsAnchor =
(getColumnModel().getSelectionModel().getAnchorSelectionIndex() == column);
javax.swing.DefaultCellEditor dce =
(javax.swing.DefaultCellEditor)table.getDefaultEditor(Object.class);
dce.setClickCountToStart(1);
import javax.swing.BorderFactory;
import java.awt.Color;
setBorder(BorderFactory.createLineBorder(Color.black);
if (comboBox.getEditor().getEditorComponent()
instanceof JTextField) {
JTextField tf =
(JTextField)comboBox.getEditor().getEditorComponent();
tf.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent fe) {
// get the text using getText() method
}});
}
public Dimension getMinimumSize() {
return new Dimension(
MINIMUM_WIDTH, MINIMUM_HEIGHT);
}
Note that the native implementation of Frame does not allow minimum size below certain size.
/**
* Reads the next token from the given stream tokenizer.
*
* @param aScanner Stream tokenizer to read from.
* @return Read string token or null.
*
* @ulcm.msg 403 getString()
* @ulcm.msg 410 EOF
* @ulcm.msg 411 EOL
*/
private String getString(StreamTokenizer aScanner) {
String rtnString = null;
try {
aScanner.nextToken();
switch (aScanner.ttype) {
case StreamTokenizer.TT_EOF:
rtnString = CommItlBundle.getString(410);
break;
case StreamTokenizer.TT_EOL:
rtnString = CommItlBundle.getString(411);
break;
case StreamTokenizer.TT_WORD:
rtnString = aScanner.sval;
break;
case StreamTokenizer.TT_NUMBER:
rtnString = String.valueOf((int) aScanner.nval);
break;
case '"':
rtnString = aScanner.sval;
break;
default:
rtnString = String.valueOf((char) aScanner.ttype);
}
} catch (IOException e) {
if (this.LOGGER.isLoggable(Level.FINER)) {
this.LOGGER.throwing(getClass().getName(),
CommItlBundle.getString(403),
e);
}
}
return rtnString;
}
cd /usr/lib
sudo tar xvfz /media/host/apache-ant-1.8.2-bin.tar.gz
sudo ln -s apache-ant-1.8.2 apache-ant
sudo update-alternatives --install /usr/bin/ant ant /usr/lib/apache-ant/bin/ant 1
sudo mkdir -p /usr/lib/jvm/
cd /usr/lib/jvm/
tar xvfz /media/host/jdk-7u2-linux-x64.tar.gz
ln -s jdk1.7.0_02 jdk
sudo apt-get install python-software-properties
sudo apt-add-repository ppa:nilarimogard/webupd8
sudo apt-get update
sudo apt-get install update-java
sudo update-java
and select /usr/lib/jvm/jdk