java

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a 'long' field. List<Long> ids = session.createCriteria(Foo.class) .setProjection(Foo#id) .list(); // The following is a compilation error, as F...

Java coding practice, runtime exceptions and this scenario

In the following scenario, I was trying to see how to handle this code and it how it relates to Runtimexception. I have read that is generally better to throw runtime exceptions as opposed to rely on static exceptions. And maybe even better to catch a static checked exception and throw an unchecked exception. Are there any scenarios w...

Java: Why does calling `remove()` on a List throw UnsupportedOperation exception?

For some reason, I'm getting an UnsupportedOpeationException with the following code. Examining it in the debugger, it looks like the object I'm calling remove() on is a list. // to optimize, remove totalSize. After taking an item from lowest, if lowest is empty, remove it from `lists` // lists are sorted to begin with public static <T ...

Java static reflection on subclasses

Hi all. I am implementing a sort of ORM in Java. I am trying to do a static find method that is only in the parent class. Let me get to the point: public class DB { public static Object find (int id) { // i want to return anew instance of the calling subclass } } public class Item extends DB { // nothing here } public class ...

Java: Most efficient way to store/retrieve workout information from a file?

I'm working on a Java project for class that stores workout information in a flat file. Each file will have the information for one exercise (BenchPress.data) that holds the time (milliseconds since epoch), weight and repetitions. Example: 1258355921365:245:12 1258355921365:245:10 1258355921365:245:8 What's the most efficient way t...

Java generics/abstract/innerclass syntax question

I've got the following: public abstract class Foo<T>{ //contents of Foo // ... public class Bar<Q> extends Foo<T>{ //contents of Foo.Bar // ... } } Later, in another class and java file, I am trying to construct an instance of the inner Bar class above, using the outer abstract class as a supertype. ...

How do I compile and run this Java Native Access (JNA) example?

I can compile this JNA example code (from step 2 of https://jna.dev.java.net/#getting_started): package com.sun.jna.examples; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; /** Simple example of JNA interface mapping and usage. */ public class HelloWorld { // This is the standard, stable way o...

Anagram Hash Function

I know something like this has been asked before, but the answer was sort of side tracked. I want to develop a hash function which will take a word and spit out an address of an array. So, for example, if you input god: sort the word, d o g perform some sort of function on this to get an address d o g -> some number insert 'dog' into...

How do I search for a String in an array of Strings using binarySearch or another method?

using binarySearch never returns the right index =/ int j = Arrays.binarySearch(keys,key); where keys is type String[] and key is type String I read something about needing to sort the Array but how do I even do that if that is the case? I wish #java was smarter =/ <-- read:simpler Given all this I really just need to know: How d...

Hibernate Component with Generics

Ok, this question is best explained in code. So will try to present the most succinct example I can. Timestamped.java @Embeddable public class Timestamped<E> { private E value; private Date timestamp; ... } Foo.java @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="TYPE") public class...

Generate Hibernate Mapping Files (*.hbm.xml) from POJOS?

What's the best/easiest way to generate *.hbm.xml files from pojos? ...

Running a Java Class from a Jar thru a command line

I have a jar file with several classes which have static main methods. Can I execute them inside the jar from the command line? If not, can I execute them one by one? ...

The jnaerator's SimpleMeaningfulExample for generating Java JNA wrappers won't work.

I tried the jnaerator SimpleMeaningfulExample (http://code.google.com/p/jnaerator/wiki/SimpleMeaningfulExample) and got: $ java -jar jnaerator-0.9.2.jar -library Test Test.h -o . -v Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLoader.defineClass1(Native Me...

Comparing java memory heap dumps: Memory profiling for Java desktop application

Hi, This is a more specific question to follow up on another question that I have asked recently. A correct answer for this question will earn a correct answer for that previous question too (since that is still in limbo)! Basically I have a Java desktop application with a memory leak issue. I am using the memory profiler in Netbeans I...

Java Code Review: Merge sorted lists into a single sorted list

I want to merge sorted lists into a single list. How is this solution? I believe it runs in O(n) time. Any glaring flaws, inefficiencies, or stylistic issues? I don't really like the idiom of setting a flag for "this is the first iteration" and using it to make sure "lowest" has a default value. Is there a better way around that? publi...

Casting from Object in Java without getting an unchecked warning

I wrote a class that has a map of <String, Object>. I need it to hold arbitrary objects, but at the same time sometimes I need to cast some of those objects, so I'll do something like HashMap<String, Object> map = new HashMap<String, Object>(); Object foo =...

Java based Atom/RSS Library that works in Google App Engine

I am trying to publish an Atom/RSS feed in my Java based Google App Engine code. I have tried using Rome and keep getting the following error (tried googling without success), also the code I am running that generates the error is the demo code (so I get the feeling Rome won't work with GAE) java.lang.NoClassDefFoundError: org/jdom/JDOM...

Growing ByteBuffer

Has anyone has ever seen an implementation of java.nio.ByteBuffer that will grow dynamically if a putX() call overruns the capacity? The reason I want to do it this way is twofold: 1) I don't know how much space I need ahead of time. 2) I'd rather not do a new ByteBuffer.allocate() then a bulk put() every time I run out of space. I co...

Nested Class In Signed Java Applet

I'v been playing with java applets recently. In the one im writing now I have a static nested class. It works fine in the viewer, but when I compile it I get 2 class files. classname.class and classname$nestedclassname.class. I've never encountered anything like this (started java a week ago) and I don't how I would jar/sign them. Also, ...

Java Path compression

Hi, I have to create a method find that would use a local Set to collect the objects and the root. Then, I would compress the object e (in the parameter) and have the roost as its parent. Then, I would return the reference to the root. I can use the Graph, Map, and set class since it was imported. But, how can I call the parent of the ro...