java

How can I cast a list using generics in Java?

Please consider the following snippet: public interface MyInterface { public int getId(); } public class MyPojo implements MyInterface { private int id; public MyPojo(int id) { this.id = id; } public int getId() { return id; } } public ArrayList<MyInterface> getMyInterfaces() { ArrayLi...

How to create Huffman tree from FFC4 (DHT) header in jpeg file?

I thought I could work this one out myself but I don't seem to be moving forward at all. Ok, the background: I need to create a Huffman tree of codes from the information provided by the FFC4, DHT (Define Huffman Table) header in a jpg file. The DHT header defines the Huffman table in this way: 1) A series of 16 bytes. Each byte defin...

Howto design for extension

There is a Checkstyle rule DesignForExtension. It says: if you have a public/protected method which is not abstract nor final nor empty it is not "designed for extension". Read the description for this rule on the Checkstyle page for the rationale. Imagine this case. I have an abstract class which defines some fields and a validate meth...

I want to upload .csv file To servlet and parse it, but dont know how to open uploaded file

I have a form that allows user to select a .csv file and post it to a servlet. However I am having dificullty getting a handle on the file from the Java Servet on the server any ideas? ...

What does it mean that Squeak runs "bit-identically" across platforms, in a way Java doesn't?

Alan Kay points out that "Unlike Java, [Squeak] runs bit-identical on every machine -- we invented this 20 years ago". The wikipedia page mentions this also: Squeak is available for many platforms, and programs produced on one platform run bit-identical on all other platforms. Since machines with different instruction sets ob...

How to prevent application freeze if server unavailable?

InetAddress serverAddr = InetAddress.getByName(serverAddress); String hostname = serverAddr.getCanonicalHostName(); Socket socket = new Socket(serverAddr, portNumber); // Freezes before this line if the server is unavailable socket.setSoTimeout(3000); Does anyone know how to implement the check of server availability or prevent the fre...

Calling a Remote Bean vs Local Bean in App Server

Is there a noticeable amount of performance overhead in using Remote Bean Interface over using a Local Bean Interface? I would like to have every Client application connect to remote beans if there is little performance difference. ...

What is the "right" way to test the output of Java methods?

In Python, I often have tests which look something like this: tests = [ (2, 4), (3, 9), (10, 100), ] for (input, expected_output) in tests: assert f(input) == expected_output What is the "right" way to write tests like this (where a set of test cases is specified, then a loop runs each of them) in Java with JUnit? Tha...

Generic wildcards in variable declarations in Scala

In Java I might do this: class MyClass { private List<? extends MyInterface> list; public void setList(List<MyImpl> l) { list = l; } } ...assuming that (MyImpl implements MyInterface) of course. What is the analog for this in Scala, when using a Buffer? import java.lang.reflect._ import scala.collection.mutable._ class Sca...

Can selenium handle autocomplete?

I have a test case that requires typing in a partial value into an ajax based textfield and verifying the list has the expected content. If it does, select the content. Any idea how to make this work? ...

Why do inner classes make private methods accessible?

I don't understand why this compiles. f() and g() are visible from the inner classes, despite being private. Are they treated special specially because they are inner classes? If A and B are not static classes, it's still the same. class NotPrivate { private static class A { private void f() { new B().g(); ...

Does cloning provide a performance improvement over constructors/factory methods?

I'm maintaing an older Java code base (jvm 1.4) that seems to use cloning as an alternative to object instantiation, I'm guessing as a performance optimization. Here's a contrived example: public class Foo { private SomeObject obj; // SomeObject implements Cloneable public Foo() { obj = new SomeObject(); obj.setField1("abc")...

Extending Eclipse's JavaEditor (to act like Vim/change KeyListener)

Introduction a.k.a. what do I intend to do feel free to skip this part, no real information is comprised in here Because of the lack of a good, free (as in speech) vim-Mode for the otherwise excellent JavaEditor in Eclipse(3.4), I'm thinking about writing one. The available solutions are: ViPlugin: commercial and not good (e.g. no vi...

Java Ordered Map

In Java, Is there an object that acts like a Map for storing and accessing key/value pairs, but can return an ordered list of keys and an ordered list of values, such that the key and value lists are in the same order? So as explanation-by-code, I'm looking for something that behaves like my fictitious OrderedMap: OrderedMap om = new O...

In Java, why can't I declare a final member (w/o initializing it) in the parent class and set its value in the subclass? How can I work around?

In a Java program, I have multiple subclasses inheriting from a parent (which is abstract). I wanted to express that every child should have a member that is set once only (which I was planning to do from the constructor). My plan was to code s.th. like this: public abstract class Parent { protected final String birthmark; } public...

Finding the class with the main method in an applet

I'm trying to use this tool https://swingexplorer.dev.java.net/ to find some information out about an applet's swing structure. Unfortunately, I didn't develop the applet. I've pulled the jars for the applet from the cache, but there are several hundred .class files in the jars, and I don't know which one has the main method. Is the...

Converting a Bidimensional Array (Numbers) Into A Dimensional Array and Viceversa on Java

Hi everyone I'm in need of help right now. I need to convert 2 dimensional array of numbers to a one dimensional array in Java. Can anyone help me? Have a great day. :) ...

What is the correct target for the JAVA_HOME envrionment variable for a Linux OpenJDK debian-based distribution?

Folks In Windows, JAVA_HOME must point to the JDK installation folder (so that JAVA_HOME/bin contains all executables and JAVA_HOME/libs contains all default jar libraries). If I download Sun's JDK bundle and installs it in Linux, it is the same procedure. However, I need to use Kubuntu's default OpenJDK package. The problem is that a...

In Java, are enum types inside a class static?

I can't seem to access instance members of the surrounding class from inside an enum, as I could from inside an inner class. Does that mean enums are static? Is there any access to the scope of the surrounding class's instance, or do I have to pass the instance into the enum's method where I need it? public class Universe { public f...

How to set up Java to use user specific certificates for Eclipse?

I can't believe I'm the only person to run up against this problem. I've been googling for hours and have not had any luck. The Java security documentation doesn't seem to address PKCS12 certificates thoroughly. I am trying to setup Java for user specific PKCS12 certificates. Among other things, this will be used so that, in Eclipse, ...