Diff in Java

A workmate of mine is currently deep involved in a big deploy. He has to take a huge amount of sql scripts out of our SCM, check them manually to discover if the other programmers have followed the internal coding standard and fix them if this is not the case.

While it is still a mistery to me why nobody though to it, I started the development of a small tool that could possibly automatically check and fix all the errors.

Part of this tool is a diff style window that show the user what the tool is fixing. It seems that diff in Java is a quite required feature, so, here’s my solution.

Obviously I didn’t reinvented the weel and my implementation is 100% based on the one published on wikibooks. My implementation consist of a couple of class.
The first one is able to give access to strings line by line.

  1. public class LineFile {
  2.  
  3.  private List lines;
  4.  
  5.  public LineFile(InputStream in) {
  6.   try {
  7.    lines = IOUtils.readLines(in);
  8.   } catch (Exception e) {
  9.    throw new RuntimeException(e);
  10.   }
  11.  }
  12.  
  13.  public LineFile(File f) {
  14.   try{
  15.    lines = IOUtils.readLines(new FileInputStream(f));
  16.   }catch(Exception e){
  17.    throw new RuntimeException(e);
  18.   }
  19.  }
  20.  
  21.  public LineFile(String from) {
  22.   try {
  23.    lines = IOUtils.readLines(new ByteArrayInputStream(from.getBytes()));
  24.   } catch (IOException e) {
  25.    throw new RuntimeException(e);
  26.   }
  27.  }
  28.  
  29. public String lineAt(int index){
  30.  if(index>=0 && index<lines.size()){
  31.  return lines.get(index);
  32.  }
  33.  return null;
  34.  }
  35.  
  36.  public int lines() {
  37.  return lines!=null ? lines.size() : 0;
  38.  }
  39. }

The second one is the actual implementation of the diff algorithm, and is based on considering each line as the most little element the algorithm can consider.

  1. public class LineFile {
  2.  
  3.  private List<String> lines;
  4.  
  5.  public LineFile(InputStream in) {
  6.   try {
  7.    lines = IOUtils.readLines(in);
  8.   } catch (Exception e) {
  9.    throw new RuntimeException(e);
  10.   }  
  11.  }
  12.  
  13.  public LineFile(File f) {
  14.   try{
  15.    lines = IOUtils.readLines(new FileInputStream(f));  
  16.   }catch(Exception e){
  17.    throw new RuntimeException(e);
  18.   }
  19.  }
  20.  
  21.  public LineFile(String from) {
  22.   try {
  23.    lines = IOUtils.readLines(new ByteArrayInputStream(from.getBytes()));
  24.   } catch (IOException e) {
  25.    throw new RuntimeException(e);
  26.   }
  27.  }
  28.  
  29.  public String lineAt(int index){
  30.   if(index>=0 && index<lines.size()){
  31.    return lines.get(index);
  32.   }
  33.   return null;
  34.  }
  35.  
  36.  public int lines() {
  37.   return lines!=null ? lines.size() : 0;
  38.  }
  39. }

Finally, to give you an idea of how it performs, here’s a couple of screenshots from the application.

If you need more details regarding the code, don’t hesitate and drop me a message!

Good environment variable values during iPhone development

Directly from GHUnit, a small note about which environment variables to set while developing for the iPhone.

Environment Variable:                 Default:  Set to:
NSDebugEnabled                           NO       YES
NSZombieEnabled                          NO       YES
NSDeallocateZombies                      NO       NO (or YES)
NSHangOnUncaughtException                NO       YES
NSAutoreleaseFreedObjectCheckEnabled     NO       YES

Actually, I keep forgetting them, so I hope this will be useful to me.

Here an example that shows how to set those environment variables.

Setting iPhone development variables in XCode

Get rid of folder that should not be versioned

If you work with subversion, for sure you had the problem to tell to subversion to ignore a folder inside your working copy. This happens for example if your IDE builds the sources in a folder inside your working copy. From the very first commit you’ll start noticing that subversion wants to add that “build” folder to your repository. But, obviously, this is not a good practice.

So you can tell subversion to ignore a folder. Just move in the folder that contains the folder you want to be ignored by subversion and type the following command.

svn propset svn:ignore <dir_you_want_to_be_ignored> .

The “dot” at the end is important! It means you’re specifying which folders of the current folder you want to be ignored.

Please note that <dir_you_want_to_be_ignored> should be written without trailing slashes. So for example if you want the build directory not to be managed by subversion, type…

svn propset svn:ignore build .

And not…

svn propset svn:ignore build/ .

This is a quick tip. I always forget how to do it, so I wrote that little note.

Chosing the number of decimal digits when printing a number with iPhone

This is a small snippet, useful whenever you want to convert a floating point number in a string with a fixed number of decimal digits.

NSNumberFormatter *format = [NSNumberFormatter new];
 [format setMaximumFractionDigits:1];
 NSNumber *aNumber = [[NSNumber alloc ] initWithDouble:54.4235264];
 NSString label = [[NSString alloc] initWithFormat:@"The number is %@", [format stringForObjectValue:aNumber]];
 [distance release];
 [format release];

Obviously the NSNumberFormatter class offers plenty of other method you can use to customize the way your numbers should be rendered.

iPhone: how to select a UITabBarItem programmatically

I’m working to an iPhone application with a TabBar on the footer area. Among the others there are a “Home” button and an “Account” button. The first allows the user to see a view that gives an overview of the application. The latter shows the view that the user uses to manage his/her account.

If the user is not currently logged in, in the “Home” view a warning message is displayed. There is also a button that is supposed to show the “Account” view. In that way it’s more clear for the user that he/she should log in.

How can you accomplish that task ? That post shows you the way I followed.

The big idea is to retrieve the TabBarController from the “Home” controller, and programmatically change the current view. To do that, the “Home” controller should get a reference to the TabBarController. But you can easily set that dependency in Interface Builder. You only need to provide a suited IBOutlet to the Home controller.

That is the tree structure of the controllers and views, as built in the interface builder.

iphone_change_tabbar_programmatically_0

So, that’s an excerpt of the HomeController showing you only what is really needed to reach our goal. The goToRegistrationPage method will execute programmatically the switch to the account page.

@interface HomeViewController : UIViewController {
UITabBarController *mainTabBarController;
}
@property(nonatomic, retain) IBOutlet UITabBarController *mainTabBarController;
-(IBAction) goToRegistrationPage;

Now, we only need to set in Interface Builder the link between the the IBOutlet we just created and the Tab Bar Controller. Here’s the result.

iphone_change_tabbar_programmatically_1As you can see the mainTabBarController outlet is linked to the Tab Bar Controller we want to manage programmatically. That’s not difficult at all. Let’s give a look to the goToRegistrationPage method.

-(IBAction) goToRegistrationPage {
 mainTabBarController.selectedIndex=3;
}

Quite easy, isn’t it ?

Now the user click on the button on the Home view and he/she get exactly the same result of touching the tab bar button in the footer.

Hope you can find it useful.