Upgrading Eclipse 4.3 Kepler to 4.4 Luna

Eclipse Luna logoEclipse 4.4 Luna was recently released along with Google’s Android ADT 23 upgrade. Just like the previous version, upgrading is fairly simple, although I ran into an error on one of my PCs that needed a manual fix. Some issues with ADT and the Android SDK can also be expected as some file paths changed from the previous version.

Upgrading Eclipse

The upgrade process is exactly the same as before: change your software repositories, check for updates and install them. If you’re using other repos, it is a good idea to export the URLs, just in case the repo settings get wiped after the update, which seems to happen often for me. It is recommended to install all updates to Kepler before upgrading to Luna.

Read my previous post with detailed instructions and screenshots if you’re not sure where to look, just make sure you use the new URLs listed here.

Eclipse 4.4 Luna repositories:

In case you’re using Eclipse for Android development, the ADT repo hasn’t changed:

As with 4.3 Kepler, the splash screen will not change on the first restart after the upgrade. Just restart the IDE again to confirm that you’re running the latest version.

Error updating eclipse.exe

One of my Windows PCs failed the update process with the following error:

An error occurred while uninstalling
session context was:(profile=SDKProfile, phase=org.eclipse.equinox.internal.p2.engine.phases.Uninstall, operand=[R]org.eclipse.sdk.ide.executable.win32.win32.x86_64 4.3.1.M20130911-1000 --> null, action=org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CleanupzipAction).
Backup of file D:\Utils\Eclipse\eclipse.exe failed.
Can not remove : D:\Utils\Eclipse\eclipse.exe

I found a couple of related bug reports here and here. At this point your Eclipse installation is only partially upgraded, follow these steps to fix the problem:

  • Restart Eclipse. Don’t be alarmed/misled by the Luna splash screen, the core module is still 4.3 (as seen on the about screen), but is should still be working.
  • While Eclipse is still running, find and kill the eclipse.exe process via the Windows task manager
  • Rename/delete eclipse.exe from your Eclipse folder.
  • Switch back to the Eclipse window and retry upgrading by clicking on Help / Check for Updates.

This time it should complete successfully and the Luna executable should be created in your Eclipse folder. After the upgrade completes, Eclipse tells you that it needs to be restarted. The IDE will close, but it won’t restart automatically. Simply run your new eclipse.exe, and your installation should be fully upgraded.

Updating Android ADT

For some reason the latest ADT refused to install as an update for me. After updating the SDK tools through the Android SDK Manager, Eclipse told me that it needed an updated version of ADT, but checking for updates found nothing. I had to force an installation of the new ADT packages as new software, then Eclipse offered to remove the old, conflicting packages:

  • In Eclipse, click on Help / Install New Software…
  • Select your ADT repo on the top, check the box next to the required packages under Developer Tools, then click Next:
  • Select the second radio option (Update my installation…), click Next:
  • Review packages, click Next:
  • Accept licenses, then click Finish to start updating:

Android compilation issues with version 23

After the upgrade, some of your Android projects might fail to build due to a build path error. The reason is that annotations.jar has been moved to a different folder in the Android SDK.

  • One possible fix is to change your project’s target API level to 19 (Android 4.4.2). You can still keep the minimum version lower in your manifest to support older Android versions in your build.
  • If you can’t or don’t want to do the above, simply copy [SDK folder]\extras\android\support\annotations\android-support-annotations.jar to [SDK folder]\tools\support\annotations.jar.

Your project should build successfully.

 

Share Button
Posted in Blog Tagged with: , , ,

Querying and filtering JavaScript objects and arrays

Another powerful feature of Mangler.js is checking if an object or value satisfies a test expression. A test expression is a javascript object literal with special properties, very similar to and inspired by MongoDB‘s .find() method.

Using test expressions, there are a number of different methods which help you filter and extract parts of your collection, as well as finding the first or last item that passes the test. Here are some quick examples:

var stock = [
	{
		id: 1001,
		name: 'Apple',
		price: 0.50,
		qty: 150,
		tags: ['fruit', 'red'],
		expiry: new Date('2014-06-16')
	},
	{
		id: 1002,
		name: 'Banana',
		price: 1.20,
		qty: 30,
		tags: ['fruit', 'yellow', 'seedless'],
		expiry: new Date('2014-07-10')
	},
	{
		id: 1003,
		name: 'Cherry',
		price: 2.50,
		qty: 220,
		tags: ['fruit', 'red'],
		expiry: new Date('2014-06-19')
	},
	{
		id: 1004,
		name: 'Grapes',
		price: 0.80,
		qty: 130,
		tags: ['fruit', 'green', 'seedless'],
		expiry: new Date('2014-07-12')
	}
];

// Find cherries by ID and return object
var cherry = Mangler(stock).first({ id: 1003 });

// Find all red fruit and put them in an array
var red_fruit = Mangler.find(stock, { tags: ['red', 'fruit'] });

// Create mangler object then narrow down list to cheap seedless fruit
var m = Mangler(stock).filter({ price: { $lt: 1.00 }, tags: ['seedless'] });

// Register custom Date getter for querying the number of days left
Mangler.registerType(Date, {
	get: function(date, key) {
		if(key === 'daysLeft') {
			var oneDay = 24 * 60 * 60 * 1000; // Milliseconds in a day
			return (date.getTime() - Date.now() + Date.now() % oneDay) / oneDay;
		}
	}
});

// Get all items that expire in the next 7 days
// As of 2014-06-15, matching items are apple and cherry
var thisWeek = Mangler.find(stock, { 'expiry.daysLeft' : { $gte: 0, $lt: 7 } });

Functions and methods

Before going deeper into the test expressions themselves, here is a list of all the functions and methods of the Mangler.js library that can be used for querying and testing. In the following spec, <value> is any javascript value, like a string, number, or object. Test expressions will be marked by <test>.

All functions and methods use Mangler.test(<value>, <test>) internally, which returns true if a single <value> satisfies the <test> expression.

Static methods of the library for testing items contained in any iterable object, such as arrays:

  • Mangler.find(<iterable>, <test>) returns an array of all items in <iterable> that passes the <test>.
  • Mangler.first(<iterable>, <test>) returns the first item of <iterable> that passes the <test>.
  • Mangler.last(<iterable>, <test>) returns the last item of <iterable> that passes the <test>.

Alternatively, you can use mangler objects to easily do multiple operations on the same set of data. They are simple object containers created and returned by the Mangler(<items>) function. On a mangler object var mg = Mangler(data), you can use the following methods:

  • mg.find(<test>) returns an array of all items in the mangler object that passes the <test>.
  • mg.first(<test>) returns the first item of the mangler object that passes the <test>.
  • mg.last(<test>) returns the last item of the mangler object that passes the <test>.
  • mg.filter(<test>) removes all items from the mangler object that doesn’t pass the <test> and returns a reference to the mangler object itself for method chaining.

I showed you in the Mangler.js introduction post how to find an item in an array by creating an index on an object property. While it is useful if you do multiple lookups on the list, for one-off searches use one of the above .first() methods, which should be more efficient.

Test expression

A <test> expression can be one of the following:

Read more ›

Share Button
Posted in Blog Tagged with: ,

Redesigning Mangler.js type handling

Mangler.js‘s type handling has undergone a major redesign which retired the flawed Mangler.getType() function and changed the type parameter on Mangler.registerType(). With the recent changes the library now supports code minification, objects built from anonymous constructors and constructors with the same name in different scopes.

Read on to see what the issue was, how it got handled, and to see the newly introduced Mangler.compareType() function for type checking.

Original implementation

Originally, Mangler.js’ type handling was based on the Mangler.getType() function, which returned a string representation of an object instance’s type:

fn.getType = function(obj) {
	var name;

	if(typeof obj === 'undefined') {
		name = 'undefined';
	} else if(obj === null) {
		name = 'null';
	} else if(typeof obj === 'object') {
		name = Object.prototype.toString.call(obj).match(/^\[object (.*)\]$/)[1];
		if(name === 'Object') {
			name = obj.constructor.toString().match(/^function ([^\(]*)\(/)[1];
			if(!name) name = '$unknown';
		}
	} else if(typeof obj === 'function') {
		name = 'function';
	} else {
		name = '$value';
	}

	return name;
}

As you can see, it extracted the type from .toString()‘s output: [object Something]. For native types it returns the type name, but for other objects it just returns a generic [object Object]. In this case I added a second step, which extracts its constructor’s function name.

The returned type string was then used to read a property from the internal types object, that contained the handler functions for cloning and iterating through the object.

When registering type handlers with Mangler.registerType(), you simply had to pass the object’s type name, matching the one returned by the above function.

Fundamental flaws

It’s easy to see how this approach cannot be reliably used in most situations:

Read more ›

Share Button
Posted in Blog Tagged with: , ,

Iterating through JavaScript objects and arrays

In this article we’ll have a look at how to iterate through javascript objects and arrays with Mangler.js. I’ll also show you how to make your own objects iterable, and how it enhances other powerful features of the library.

Iterating through object literals and arrays

By default Mangler.js recognises 3 types of objects as iterable: object literals, arrays and mangler objects. To iterate through these objects, simply call Mangler.each() and pass a callback function in the form of callback(key, value):

var order = [
	{ product: 1, qty: 2, price: 1.00 },
	{ product: 2, qty: 1, price: 1.50 },
	{ product: 3, qty: 3, price: 2.00 }
];

var total = 0;
Mangler.each(order, function(index, item) {
	total += item.price * item.qty;
});

The callback function will be called for every key/value pair:

  • Arrays: The callback is called for every item in the array, passing the numeric item index as key and the item itself as the value.
  • Objects: The callback is called for every object property, passing the property name for the key as string and the property value.

If the callback function returns false, iteration will stop.

Recursive iteration

With Mangler.explore(), you can recursively iterate through all iterable children as well. In addition to the key and the value, the callback function gets a path and a state parameter too:

  • The path is a string containing the absolute path of the current item’s parent from the root of the iteration, e.g. root.array[0].parent.
  • The state is an optional object which is passed down the hierarchy. It is shallow copied at every level, any changes made to it are only visible to the current item’s siblings and children.

Read more ›

Share Button
Posted in Blog Tagged with: ,

Cloning JavaScript objects with Mangler.js

I was pretty busy lately watching all 5 seasons of Breaking Bad, but there were still quite a few improvements to Mangler.js since my last post. With many features taking final shape, it is time to write some posts focusing on when and how to use it, and why it might be useful.

In the introduction post I wrote a sneak-peak of how to extract parts of your data and how to create indices from object properties. I’ll get back to these topics in the future in more detail.

In this post I’ll use Mangler.js to create deep and shallow copies of objects.

Cloning (deep copy)

While many Mangler.js functions attempt to preserve references to the original data, sometimes you need an exact copy you can freely mess with without touching the original. The Mangler.clone() utility function creates and returns a deep copy of any object you pass to it:

var data = {
	id: 1,
	text: 'Hello world',
	fruit: ['apple', 'orange', 'banana'],
	now: new Date()
};

var dataClone = Mangler.clone(data);

The passed object will be processed recursively and the returned object will be an identical copy with no references pointing back to the original. Well, with one exception…

By default Mangler.js knows how to clone simple javascript objects and arrays, as well as Date objects and mangler objects. It is more than enough for processing simple JSON and literals, but if your data contains other object instances, they will not be cloned. If Mangler.js encounters an object it doesn’t recognise, it will pass its reference to the clone as is, pointing to the original object. Luckily, there is a way to teach Mangler.js how to clone other objects.

Cloning object instances

If you want to clone data that contains other native javascript types like typed arrays and Error objects, you can simply use the Mangler-natives module to add cloning support. Simply include mangler-natives.js after mangler.js to use. In case of your own types, you have to write the cloning function yourself and register it with Mangler.js:

// Define simple person object
function Person(firstname, surname) {
	this.firstname = firstname;
	this.surname = surname;
}

// Register cloning function
Mangler.registerType(Person, {
	clone: function(obj) {
		return new Person(obj.firstname, obj.surname);
	}
});

// Mangler.js can now clone person object instances
var person = new Person('John', 'Smith');
var personClone = Mangler.clone(person);

The above example creates a very simple Person object and specifies a cloning function to use. After calling .registerType(), Mangler.js will properly clone Person objects anywhere it finds it.

Standard cloning methods

In addition to a custom clone function, there are a couple of simple registration methods you can use if your object supports them.

Read more ›

Share Button
Posted in Blog Tagged with: ,

Excavation of an extra-terrestrial

E.T. the Extra-Terrestrial was released to cinemas in 1982. The critically acclaimed movie surpassed Star Wars to be the highest grossing film of all time until 1993′s Jurassic Park. For gamers however, its video game adaptation is more famous for being a symbol of the 1983 video game crash and a reminder for bad business practices.

E.T. in-game screenshot

E.T. in-game screenshot

The year of crap games

E.T. CoverThe E.T. video game was released in December 1982 by Atari Inc, who manufactured 5 million cartridges out of which only 1.5 million was sold. Thanks to its horrible gameplay and crap graphics, it is considered to be one of the worst video games of all time. And yes, its graphics was bashed even by 1982 standards. It was released in the holiday season and managed to climb the charts as being one of the best-selling Atari 2600 titles, but the 3.5 million unsold copies and the unusually high $20+ million licensing fee definitely didn’t help its success.

It wasn’t the only title that faced difficulties that year: 9 months earlier Atari released the 2600 port of the famous arcade game, Pac-Man. Confident that it would boost console sales they manufactured 12 million cartridges for the 10 million consoles on the market. 7 million copies were eventually sold, but because of the poor gameplay many gamers started returning them to the store.

Due to these commercial failures, the previously dynamically growing company began to wobble and was eventually sold off in 1984. They learned that you can’t treat gamers like crap with unfinished rubbish titles and can’t just cram anything down their throats and expect them to pay for it. Retailers also got more crafty, demanding return policies for unsold products, not wanting to take the hit for unfair bundle deals full of worthless, unsellable games.

Burial

As a result of all this, by September 1983 Atari amassed a huge amount of worthless digital junk in their warehouse in El Paso, Texas. They filled several trucks and took them to a landfill in Alamogordo, New Mexico, where it was buried. They even had to pour concrete over it to stop kids digging and looting the grave.

Despite it was reported from several sources, it is still somewhat of an urban legend, and nobody knows for sure what’s actually down there. Apart from unsold Pac-Man and E.T. cartridges, there are probably other games and unsold console systems buried as well.

Excavation

In 2013 Fuel Entertainment acquired the rights to excavate the site. After some legal troubles were settled, it is now official that excavation will take place on 26th April 2014 between 9:30am and 7:30pm. It is a public event and a documentary will be shot to reveal the details of Atari’s burial.

Make sure you don’t miss the reports.

UPDATE: It is indeed confirmed, not a big surprise though.

Share Button
Posted in Blog Tagged with: , ,

NumGuess in VB.NET and F#

Two more NumGuess versions have been added to the repository. VB.NET was strangely missed so far, should have been added when making the C# version, as it’s mostly identical apart from the syntax. The other one is written in the functional F# language (ok, it’s multi-paradigm). Enjoy the latest additions, and let us know if there is another language that you’d like to see for your favourite number guessing game.

Share Button
Posted in Blog Tagged with:

Mangler.js documentation

It took a bit of time, but the Mangler.js documentation is finally online. As the project is still in its early stages, it could become outdated quickly and needs regular maintenance, but as of now, it is a complete API reference of all current features.

If you have any questions, post a comment below, or if you found a problem with the docs, create an issue in the GitHub repo.

Share Button
Posted in Blog Tagged with:

Introducing Mangler.js

Mangler.js is a JavaScript object processing library that helps you transform, restructure, query, index and extract parts of your objects and JSON data. It is open source under GPLv3.

As I often work with bespoke applications where requirements change regularly, it is sometimes impossible to foresee what will need to be added in a month’s time. Due to short deadlines, there is no time to rewrite hundreds of lines of code to re-shape and re-optimise the data, just because a new JSON datasource with unexpected structure has to be integrated. This often leads to one-off conversion functions, which are a pain to write and has no use anywhere else. This library aims to help with these situations.

The project is currently in pre-alpha preview state, keep an eye on the Mangler.js project page for future updates. Documentation is being worked on, new features and breaking changes can be introduced regularly. You can download the most recent version from the Mangler.js GitHub repo.

Data extraction

So far the most useful feature of Mangler.js is the .extract() method, which creates a new slice of your data while attempting to keep object references intact. Suppose you have the following nested object:

var data = {
	mobile_os: [
		{ id: "001", name: "Android" },
		{ id: "002", name: "iOS" }
	],
	desktop_os: [
		{ id: "003", name: "Windows" },
		{ id: "004", name: "Linux", sub_os: [
			{ id: "005", name: "CentOS" },
			{ id: "006", name: "Ubuntu" }
		]}
	]
}

To extract all mobile operating systems into an array, simply include mangler.js in you code and call the .extract() method with a filter:

var os = Mangler(data).extract('mobile_os');

Simply pass the object to the Mangler() function to return a mangler object. Many mangler object methods return a reference to the mangler object itself, so you can conveniently chain methods together. The .extract() method traverses the whole object and extracts items matching the filter. To access the items in a mangler object, read its .items property. In this example, here’s what os.items looks like:

[
	{ id: "001", name: "Android" },
	{ id: "002", name: "iOS" }
]

Filters can be as vague or as specific as you want them to be and can match anything anywhere in the object, not just top-level properties:

var os;

// Extract all name properties from all levels
os = Mangler(data).extract('name');
// os.items are ["Android", "iOS", "Windows", "Linux", "CentOS", "Ubuntu"]

// Extract all mobile OS names
os = Mangler(data).extract('mobile_os[].name');
// os.items are ["Android", "iOS"]

// Extract all linux distro names
os = Mangler(data).extract('desktop_os[1].sub_os[].name');
// os.items are ["CentOS", "Ubuntu"]

Filters also support wildcard characters, use * to substitute zero or more levels of references:

// Extract all desktop OS names, including distros
os = Mangler(data).extract('desktop_os.*.name');
// os.items are ["Windows", "Linux", "CentOS", "Ubuntu"]

You can also use ? anywhere to match partial property names. The filter ?_os collects top-level objects from both the mobile_os and desktop_os arrays, yielding the following:

[
	{ id: "001", name: "Android" },
	{ id: "002", name: "iOS" },
	{ id: "003", name: "Windows" },
	{ id: "004", name: "Linux", sub_os: [
		{ id: "005", name: "CentOS" },
		{ id: "006", name: "Ubuntu" }
	]}
]

The .extract() method supports additional options as the second parameter. By default processing will stop when the filter matches and it won’t go deeper into the objects. In the example above even though sub_os would match the filter, its items are not extracted as desktop_os already matched the filter higher up in the hierarchy. To override this behaviour, use the drilldown option:

os = Mangler(data).extract('?_os', { drilldown: true });

which results in the following items array:

[
	{ id: "001", name: "Android" },
	{ id: "002", name: "iOS" },
	{ id: "003", name: "Windows" },
	{ id: "004", name: "Linux", sub_os: [
		{ id: "005", name: "CentOS" },
		{ id: "006", name: "Ubuntu" }
	]},
	{ id: "005", name: "CentOS" },
	{ id: "006", name: "Ubuntu" }
]

Indexing

I mentioned earlier that Mangler.js tries to preserve your object references. It means while the original data object is untouched, all items in the mangler object are still referencing parts of it. Changing the properties of the extracted objects WILL change the original data.

Let’s say we want to rename Linux to GNU/Linux. Items could in theory be in any order in the data, so we need to find the object we want to change by its id property. The easiest way to do it is to call the .index() method, which returns a JavaScript object keyed on the required property:

// Get a list of items in an object keyed on id
var idx = Mangler(data).extract('?_os').index('id');

// Change name
idx['004'].name = 'GNU/Linux';

Here’s how the resulting idx object looks like:

{
	"001": { id: "001", name: "Android" },
	"002": { id: "002", name: "iOS" },
	"003": { id: "003", name: "Windows" },
	"004": { id: "004", name: "GNU/Linux", sub_os: [
		{ id: "005", name: "CentOS" },
		{ id: "006", name: "Ubuntu" }
	]}
}

Because of the preserved references, you’ll see that the name didn’t just change in the mangler object, but in the original data as well, while the original data’s structure is still intact.

Wait, there’s more…

It was only a sneak peek of some of the already included features, watch out for more updates to come in the near future, as well as more posts covering extra features. I’m still working to put some documentation together, in the meantime you can have a look at the source code in the GitHub repo.

Share Button
Posted in Blog Tagged with: , , , ,

Android 4.3 developer options missing

Last week Three UK has finally rolled out the Android 4.3 update for Samsung Galaxy S3 phones. It was a massive 450 Mb download, and the update process completed without problems.

It has been reported last year that Samsung postponed the update due to issues reported by customers. Hopefully most have been ironed out since, the only issue I noticed was the occasional split-second audio gap while playing music in the background.

Some apps also received an update, but nothing major. The one I like the least is the update to the calendar widget: I wasn’t a big fan of the light-brown theme it had before to match the organiser app, but now it’s sporting a green and white theme, which doesn’t match the look of anything at all. I’m still looking for a nice looking translucent dark 4×2 widget on the Play Store which nicely handles calendar events and tasks, but found none so far.

Developer Options

Being a developer I also noticed that the developer options are missing from the settings page. That is where you can allow programs from unknown sources, enable USB debugging, and other handy options.

Developer options are hidden on Android 4.3 by default. To enable it, you have to go to Settings / More tab / About device, scroll down to the bottom and tap on the seemingly disabled Build number 7 times. After that, it will sit in the settings menu as usual.

Enabling Android 4.3 developer options

Tap-tap-tap-tap-tap-tap-tap, and you’re set.

Now I just want to go around and randomly tap disabled menu items to see what else can be found.:)

Share Button
Posted in Blog Tagged with: ,