Tuesday, June 10, 2014

Git Checkout to a Specific Commit

In Git, to get your workspace code to that of a specific commit:

git checkout 0fe3207

To go back to HEAD e.g. in the "master" branch:

git checkout master

References:

Saturday, May 31, 2014

Android ArrayAdapter getView's "convertView" Parameter

The ListView and ArrayAdapter doesn't create a new View for every item in the list. Instead, view items are pooled and recycled as the user scrolls through the list. So what "getView" does is that sometimes it requires you to create a new View (when "convertView" is null) or to re-use an existing view (when "convertView" is not null).

Therefore, in getView, you should do a null check for convertView. If it is null, create a new View, otherwise, just reuse it.

Monday, May 26, 2014

Android Setting Layout Parameters Programmatically

This is how it's done:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.setMargins(10, 0, 0, 0);

But the catch is, if you are adding these views programmatically in the first place, you will need to ensure that the above code runs only after the views have been added to the parent.

Android Design Resolution

If using "dp" i.e. density independent pixels, dp values specified in the app should be based on a 320x480 design resolution.

Sunday, May 25, 2014

Android Center Vertically in Parent

Instinctively, it's to use this, but sometimes it works, and sometimes it doesn't work:
android:layout_centerVertical="true"

When it doesn't work, try this:
android:layout_gravity="center_vertical"

Not sure why but that's just how it is.

Monday, May 12, 2014

AngularJS Two Way Data Binding Only Works One Way Error

There is a problem with two way data binding in AngularJS where the data binding only works one way. This happens when the HTML element bound to the data is inside an automatically created child scope (e.g. inside an ng-include or ng-repeat block).

Let's say the following code is within an ng-include block:


<input ng-model="valueX" />

If $scope.valueX is set as "100" in the controller, it will show "100" in the input box above. However, if the user updates the value, this value won't be reflected in "$scope.valueX", because in this case the child scope will create a copy of that, different from the parent scope value. This results in two way data binding only working one way.

One solution is to use objects rather than primitives e.g.

<input ng-model="anObject.valueX" />

But it does not work all the time. In the case it doesn't work, another option is to explicitly reference the $parent scope:

<input ng-model="$parent.anObject.valueX" />

Reference: https://github.com/angular/angular.js/wiki/Understanding-Scopes

Tuesday, April 22, 2014

MongoDB MapReduce Notes

A few things that stumped me for a while and took me some time to figure out:

  1. Each iteration of the Map step applies only to one document. However, there is a lot of flexibility in terms of what gets emitted in iteration. It can either emit multiple items, or none if certain conditions are not met.
  2. It is also completely up to the user to create the appropriate key in the Map step. The key thing is that all items with the same key (regardless of which document it originated from) all funnels into the same Reduce iteration eventually (it is possible for e.g. 200 values to be reduce to 1 value in multiple steps, with the reduce output from previous steps appearing in the list as one of the items).
  3. The Reduce function will not be called for keys that have single values. This means that the same object structure should be used throughout the process.
  4. There is a "scope" attribute which can be used for global parameters.

Saturday, April 19, 2014

MongoDB Collection Counting

This is done via the method "count()"

db.userAccount.count()

This can be extended to perform conditional counting:

e.g. number of users in New York city

db.userAccount.count({"city":"New York"})

More examples and references: http://docs.mongodb.org/manual/reference/method/db.collection.count/

MongoDB Limit Fields to Return

This is done using the projection feature. Some examples:

To find all entries in userAccount but only have the email returned:

db.userAccount.find({},{"email":"1"});

To find all entries in userAccount from city "New York" and only have the email returned:

db.userAccount.find({"city","New York"},{"email":"1"});

0 can be used to exclude the field

More examples and references: http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/

Tuesday, March 25, 2014

Monday, March 3, 2014

MacOS Finder Remove Folder on Toolbar

In MacOS, it is possible to add a folder to a toolbar. I don't find this feature useful but sometimes it can happen by accident when trying to click and drag a window. To remove it, CMD-Click and drag the folder out.

Thursday, February 27, 2014

C++ Avoid Circular Header File Dependencies

In C++, you will get compile errors when header files have a circular dependence on each other. Therefore, it is best to use forward declarations in the header files, and include the header files only in the "cpp" definition files.

However, there are 2 exceptions to this, where you will need to include another header file in the header file:

  1. When you are inheriting from another class, you will need the header class for that class in the header file
  2. When you are using a class as a stack variable rather than dynamically allocated heap variable, you will also need the header file for that class in the header file

Saturday, February 22, 2014

Article on Coordinate Unit Movement

This article provides an in-depth writeup on the various issues, solutions and options relating to multiple units moving in a coordinated way in a game: http://www.gamasutra.com/view/feature/131721/implementing_coordinated_movement.php?print=1

Issues covered include formation movement, and difficult problems such as the "stacked canyon" problem.

Friday, February 21, 2014

SSL Certificate Types

After some changes in 2007, there are now 2 classes of SSL certs:

  1. Domain Verification: only domain ownership verification based on email and/or phone and WHOIS record
  2. Extended Verification: verification of both domain ownership and business entity
DV is cheaper and most CAs issue them within hours. EV requires a few working days and costs more.

In terms of user experience if it's EV, there's a nice looking green bar in some browsers like Chrome. But there's no warning whatsoever for DV. So DV looks to be enough in most cases.

Resources:

Thursday, February 6, 2014

MongoDB Commands for Updating Values Inside Document

This page provides a good compilation: http://jonathanhui.com/mongodb-data-update

MongoDB Updating Entry in Array Within Document

This is done via the "$pop" operation but most examples out there operate on simple objects. Here's how it's done for an array that's deeper within the document.

So e.g. if we have a document like this in the "workshop" collection:


{
"_id" : ObjectId("51e216e55cf2ff1231dee123"),
"info" : {
"name" : “Workshop X”,
"city" : “Sacramento”
}
"inventory" : {
"cars" : [
{
"licensePlate" : "XXXX01",
"make" : “Toyota”
},
{
"licensePlate" : "XXXX02",
"make" : “Kia”
},
{
"licensePlate" : "XXXX03",
"make" : “Hyundai”
},
{
"licensePlate" : "XXXX04",
"make" : “Nissan”
},
{
"licensePlate" : "XXXX05",
"make" : “Honda”
}
]
}
}



We could remove the entry at the bottom of the array with the command:

db.workshop.update({"info.name":"Workshop X"} ,{$pop:{"inventory.cars" : 1}});

Note that if you use the dot notation for accessing attributes deeper into the document, you'll need to surround the expression with quotes.

More documentation on $pop here: http://docs.mongodb.org/manual/reference/operator/update/pop/#up._S_pop

Friday, January 17, 2014

Eclipse Ant JRE

Changing the JRE settings in Eclipse preferences won't affect the JRE version used by Ant. To change it, you need to right click the build file in the Ant view, click "Run As" and then click "External Tools Configurations", and then change the JRE from there.