Skip to content


The Most Efficent Way to Increment A Map Value in Java

Finding myself in a situation of needing to increment values in a map in Java, I was curious to see what the most efficient way to do so. Said piece of code will be looped over thousands of times and I wanted it to run as quickly possible. Yes, I know that pre-optimization is the root of all evil, but in this case it was something I needed to be conscious of.

I found a question/thread on StackOverflow where someone else had already pondered the same thing and got a long list of possible answers to compare to each other, including using Apache Commons and the Google Collections Library. After testing all of them, the fastest way ended-up being implementing a "MutableInt" class and using it as the value data type in the map:

CODE:
  1. class MutableInt {
  2.   int value = 0;
  3.   public void inc () { ++value; }
  4.   public int get () { return value; }
  5. }
  6.  
  7. Map<string ,MutableInt> map = new HashMap<string ,MutableInt>();
  8. MutableInt value = map.get (key);
  9. if (value == null) {
  10.   value = new MutableInt ();
  11.   map.put (key, value);
  12. } else {
  13.   value.inc ();
  14. }

Pretty interesting stuff! Note that the above code snippet is not very cleanly formatted, but you'll get the idea.

Posted in Algorithms, Computer Science, Culture, Data Structures, Disciplines, Java, Languages, Tips, Hacks, & Tricks. Tagged with , , , , , , .

Fixing Remote Desktop in Snow Leopard

After upgrading to Snow Leopard on my MacBook Pro, I only found a couple of minor issues which were easily fixed. One of them was Microsoft's Remote Desktop Client crashing at startup when I ran it. Luckily the fix was very simple-- I simply downloaded the latest version from Microsoft and installed it again. If you're having problems with Remote Desktop in Snow Leopard, give it a try!

Microsoft Remote Desktop for OS X

Alternatively, you can also try downloading CoRD, an open source Remote Desktop Client and installing it. It seemed to have trouble connecting to my Windows XP workstation for whatever reason, but that doesn't mean it won't work for you of course.

CoRD

CoRD is a Mac OS X remote desktop client for Microsoft Windows computers using the RDP protocol. It's easy to use, fast, and free for anyone to use or modify.

Posted in Culture, OS X, Operating Systems, Tips, Hacks, & Tricks, Windows. Tagged with , , , , , , .

Using Spring and Hibernate With JPA And Entity Interfaces

I'm currently setting up a new Hibernate project and finally getting around to trying out JPA annotations instead of using any Hibernate configuration or mapping files-- what the hell took me so long!!??! Anyhow, among the many gotchas I came across, one was how to deal with using Interfaces with Hibernate entity beans and associations.

When you declare an association to a Hibernate Entity with an Interface without using the correct annotation syntax to point Hibernate at the implementing class, you will likely see exceptions like this while running JUnit 4 tests in Eclipse:

CODE:
  1. java.lang.IllegalStateException: Failed to load ApplicationContext
  2.     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
  3.     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
  4.     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
  5.     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
  6.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
  7.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
  8.     at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
  9.     at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
  10.     at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
  11.     at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
  12.     at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
  13.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
  14.     at org.junit.internal.runners.CompositeRunner.runChildren(CompositeRunner.java:33)
  15.     at org.junit.runners.Suite.access$000(Suite.java:26)
  16.     at org.junit.runners.Suite$1.run(Suite.java:93)
  17.     at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
  18.     at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
  19.     at org.junit.runners.Suite.run(Suite.java:91)
  20.     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
  21.     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  22.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
  23.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
  24.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
  25.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
  26. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geoSessionFactory' defined in class path resource [com/peerdigital/spring/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.peerdigital.geolocation.model.DefaultCountryEntityImpl.regionEntity references an unknown entity: com.peerdigital.geolocation.model.DefaultRegionEntityImpl
  27.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
  28.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
  29.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
  30.     at java.security.AccessController.doPrivileged(Native Method)
  31.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
  32.     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
  33.     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  34.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
  35.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
  36.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
  37.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
  38.     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
  39.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
  40.     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
  41.     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
  42.     at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
  43.     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
  44.     ... 23 more
  45. Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.peerdigital.geolocation.model.DefaultCountryEntityImpl.regionEntity references an unknown entity: com.peerdigital.geolocation.model.DefaultRegionEntityImpl
  46.     at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
  47.     at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
  48.     at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:438)
  49.     at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:309)
  50.     at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
  51.     at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:673)
  52.     at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
  53.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
  54.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
  55.     ... 39 more

Most of the solutions I saw made you create a duplicate persistence.xml that had all of the same information in it which seemed silly to me. Eventually with the help of Google I came across the answer somewhere (which I no longer have around) which was to modify the field annotation to point at the class which implements the Interface:

CODE:
  1. @ManyToOne(targetEntity = DefaultRegionEntityImpl.class)
  2. @JoinColumn(name = "region_code_fk", referencedColumnName = "region_code_pk", insertable = false, updatable = false)
  3. private RegionEntity regionEntity;

One other thing to check-- make sure your @Entity annotation is importing the JPA annotation (javax.persistence.Entity), and NOT the Hibernate annotation (org.hibernate.annotations.Entity). If you import the Hibernate @Entity annotation it will not find the entity bean.

Hopefully that's of some help to someone. Just a quick post-- back to work I go!

Posted in Frameworks, Hibernate, Java, Languages, Spring. Tagged with , , , , , .

How To Install Dig in Ubuntu Linux

One tool I'm used to having without installing (it's included by default in FreeBSD) is Dig (which is short for Domain Information Groper). Dig is a really handy tool for checking and troubleshooting DNS related issues once you learn how to use it. For instance, it makes it easy to see how your MX records are resolving:

CODE:
  1. # dig google.com mx
  2.  
  3. ; <<>> DiG 9.3.2-P2.1 <<>> google.com mx
  4. ;; global options:  printcmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41334
  7. ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
  8.  
  9. ;; QUESTION SECTION:
  10. ;google.com.                    IN      MX
  11.  
  12. ;; ANSWER SECTION:
  13. google.com.             10212   IN      MX      10 smtp3.google.com.
  14. google.com.             10212   IN      MX      10 smtp4.google.com.
  15. google.com.             10212   IN      MX      10 smtp1.google.com.
  16. google.com.             10212   IN      MX      10 smtp2.google.com.
  17.  
  18. ;; Query time: 1 msec
  19. ;; SERVER: 208.67.220.220#53(208.67.220.220)
  20. ;; WHEN: Sat Nov  8 02:56:08 2008
  21. ;; MSG SIZE  rcvd: 116

Luckily this is easy to install, just not terribly intuitive or easy to find as it's not a package known as dig:

CODE:
  1. # sudo apt-get install dnsutils

That's pretty much it-- you should now be able to use dig!

Posted in Culture, Linux, Operating Systems, Tips, Hacks, & Tricks, Tools. Tagged with , , , , , , .

All Files Up To Date

Wow, it's really been quite awhile since I've wrote much regularly, and I hope to start reversing that trend. Ever since I f'ed up the Wordpress theme I did from scratch I've been pretty unmotivated to do much at this blog. I've looked through a zillion themes and found pretty glaring flaws in all of them, and didn't want to do one from scratch again. I was browsing around at random over the weekend and came across the new theme you see now, Carrington, at Crowd Favorite (which is local to me). Next thing you know, I've upgraded all of my blogs to the latest version of Wordpress, and even made this one look good again. It still needs a little work, but I'm happy so far. Hats off to Alex King and team at Crowd Favorite by producing a theme good enough to get me interested in my blog again.

At the same time I'm now working at BT working on high-end video teleconferencing solutions which has proved to be a really interesting situation for me-- the business knowledge needed to work in the domain itself is more challenging than becoming fluent in a new programming language. Coming from a background of working on mostly scalable services meant for website and enterprise consumption, working in a rapidly growing and changing area of telecom has proved to be a welcome new challenge. We have a lot of really cool stuff rolling around, and I can honestly say some of the stuff I'm working on actually changes peoples lives all across the world in a significant way which is a first for me, and a rare experience as a Software Engineer.

That said, I haven't had many burning topics on my mind in the software space since I've had my brain wrapped around learning how telecom and video conferencing works for the past several months. I have a couple of personal pet projects I'm working on in my spare time, and if/as they progress, they will provide some fodder for more technical blog posts.

Anyhow, thanks for reading and keeping me in your RSS feeds-- hopefully this will start getting a little more regular again soon.

Posted in Culture, Site News. Tagged with , , , .

An Alternative To Waiting In Line for the iPhone 3G

I've been on the fence quite awhile about the iPhone. It's not cheap, it's grossly over-hyped by a very fanatic fan base who seem to overlook it's flaws, and while I'm not a terribly flashy person, there seems to be a stigma about those who own one. Those things aside, I finally decided that I've pretty much bought into the Mac platform and OS X workflow, and it only makes sense to extend that by replacing my Samsung Blackjack with an iPhone. iPhone 3G Stock in Colorado The addition of 3G network support, ActiveSync, and GPS pretty much sealed the deal for me, though the price still makes me reel a bit. One of the advantages I saw with owning one is that I would most likely not need to take my laptop with me any longer on non-business trips, which is a pretty big win for me.

If you weren't willing to camp-out for the new iPhone 3G and want one ASAP, it's likely you're in a similar position as me-- calling local Apple Stores and AT&T stores everyday to check to see if any have came in. It sounds like at many Apple Store locations, as soon as a shipment comes in, a big line forms and if you're lucky you might be able to get one. The way that Apple and AT&T have handled this situation is truly awful and unprofessional-- none of the people working the floor know when shipments are coming-in, nor is there a way to order an iPhone online, etc.

One option I ended-up opting for was ordering a new iPhone 3G at my local AT&T store through their "direct fulfillment" program. Basically this boils down to you having a direct order from Apple which will eventually arrive at your local AT&T Store, and you don't get charged for it until it ships. You get a tracking number and a way to check the status, though so far the results don't look terribly promising. When I ordered mine the wait time before shipment told to me was 5-10 business days, and lately from what I've read the current quotes are now at 10-20 business days.

I honestly don't know how this is going to end-up considering the HUGE thread about this program happening at the AT&T Forums at the moment, but it is keeping me from being obsessed about trying to snag a phone at the retail locations.

One other tip, if you do opt to go this route, you can use this site to get an idea about when your order will arrive based on other orders at the same store. All that you do is enter order ID's close to the last 5 digits of your Order ID and the zip code of store you ordered from, and browse through the orders at your store to see if other people at your AT&T store have got their orders. It's kind of hard to describe unless you've went through the order process itself, but you can find out more in in this thread.

Posted in A Day In The Life Of, Culture, Tech News. Tagged with , , , , , , , .

A Regular Expression To Proxy Basic ColdFusion Requests

(Note: I'm cleaning up some old drafts which have been sitting around for awhile.)

A couple of years ago while setting-up a new J2EE server cluster using the Implementing Multitier Hardware Load Balancing with ColdFusion MX for J2EE or JRun article, I needed to setup a proxy filter for ColdFusion requests. Given that the application I was setting up used a context root of /, the proxy rule listed in that article would not work. Instead, thanks to my former co-worker Rod, I now have a regular expression which will redirect all basic requests for ColdFusion:

CODE:
  1. ^((?:\S*.cfml?\S*)|\S*/(?:\?\S+)?)$

This regexp works for the following combinations:

CODE:
  1. hello.cfm?hello=2
  2. hello/
  3. sadfa/
  4. dasd/?hello=231
  5. xo.cfm/hello
  6. hello/dookie.cfm
  7. dookie/hello.cfm?thisvar=dookie
  8. /

And the following requests are not proxied:

CODE:
  1. /hello.html
  2. hello.html
  3. hello/hello.html

Obviously these are just basic test cases, but it should leave all CSS, image, JavaScript, etc. type requests to the webserver, and pass on everything else to the app servers. You would also need to change it to include direct requests for .cfc files if you're doing anything like Flash Remoting, Web Services, etc directly to CFC's.

Posted in ColdFusion, Tips, Hacks, & Tricks. Tagged with , , , , , , , .

How To Fix Sound Problems In Ubuntu 7.10 Gutsy on Dell Computers

Currently, my main workstation at home is a Dell Dimension E520 which primarily runs Ubuntu 7.10, but I also dual boot into Windows XP for my photography hobby. One problem I had after upgrading to Ubuntu 7.10 from the default Ubuntu install which the E520 shipped with was getting sound to work. After trying several solutions in this forum thread, I finally got it working again.

Posted in Linux, Operating Systems, Tips, Hacks, & Tricks. Tagged with , , , , , , , , , .

Getting Behind Python: Sun Hires Python & Jython Developers

As both a fan and user of the great technologies Python, and the Sun JVM (primarily via Java), jython.png I was very happy to come across this eWeek article which says that Sun announced the hiring of two key Python engineers. You can read more about the hiring of Ted Leung and Frank Wierzbicki at their respective blogs.

I had pretty much written off Jython as being dead quite some time ago, but luckily it has had a lot of recent activity and is starting to catch back up with C-Python. By both hiring key JRuby and Jython developers, it looks like Sun is making sure the JVM stays relevant beyond Java and continues to evolve as what in my opinion is the best option for cross-platform applications.

Having a long background in ColdFusion (an Adobe language which compiles down to Java bytecode and runs on the JVM), I've seen first hand the benefits of moving a language to the JVM, and I look forward to seeing more progress on both JRuby and Jython on the JVM.

Posted in ColdFusion, Culture, Disciplines, Django, Frameworks, Java, Languages, Python, Ruby, Ruby On Rails, Software Engineering, Tech News. Tagged with , , , , , , .

How To Display Which Processes Are Using What Ports

This is just a quick entry on how to see which software is using which ports. This comes in handy when trying to install an application server, web server, etc, and are getting errors like "port is in use".

Basically in any Unix type derivative such as Linux such (Ubuntu, RedHat, SuSe, etc.), as well as Mac OS X, all that you need to type this at the command line:

CODE:
  1. lsof -i

I remember there being a couple of commands in Windows which you could do this with, but it's been so long since I've used Windows on a regular basis I honestly don't remember how to do it. I do know you can use TCPView to accomplish the same thing, however.

Posted in Apache HTTPD, Apache Tomcat, Culture, FreeBSD, JBoss, Linux, MySQL, OS X, Operating Systems, PostgreSQL, Servers, Tips, Hacks, & Tricks, Windows. Tagged with , , , , , , , , , , , , .