Yearly Archives: 2016


Windows: How to start “Remote Desktop Connection” without the icon shortcut

On a Windows machine we were using recently, all shortcuts icons to system applications got corrupt and they were not linking to the applications they were supposed to link to.

We needed to use Microsoft Remote Desktop Connection without the shortcuts icons. To do so we used the shortcut key of the Run Command screen that allows us to start any application that Windows knows where the binary is.

We pressed the keys Windows+R on the keyboard together and that showed the run command screen. In the input line we typed mstsc and hit the Enter button (you can also click on the OK button on the screen).

Run-mstsc

mstsc is the name that the executable file of Microsoft Remote Desktop Connection application holds in C:\Windows\System32. The easiest way to find the name of a binary is to check the icon shortcut on a machine that it is not corrupted or just search the internet for it.

Using all monitors

If you have multiple monitors that are arranged to form a rectangle, you can instruct mstsc by using the /span parameter to match the Remote Desktop width and height with the local virtual desktop and span across multiple monitors if necessary.

Executing mstsc /span will create a bigger window to the remote machine that will cover all your local monitors. Unfortunately, this window will not operate as if it is composed of multiple monitors. If you set a window to full screen, it will fill ALL local monitors as if they are one.


WordPress: Google AdSense Plugin 3

Recently, we were trying to changing the position of the Google Ads that are shown on a website using the Google AdSense plugin for WordPress.

After clicking on the Manage Ads button at the configuration page (http://example.com/wp-admin/options-general.php?page=GooglePublisherPlugin) we would get an error that another theme had set the active ads and that the plugin cannot proceed to modify them unless they are disabled from the other theme (Specifically we would get the following error: Due to a theme change, ads are not being shown. Please reconfigure your ads or restore the previous theme.).

(a) Disabling the plugin, (b) Uninstalling the plugin (c) Reconnecting with another AdSense account did not help at all.

What we did at the end was to remove the GooglePublisherPlugin entry from the wp_option database of our installation.

DELETE FROM `MyDatabase`.`wp_options` WHERE `option_name` = "GooglePublisherPlugin";

Next time we visited the configuration page, we were disconnected from the previous account and we had to reconnect.

GetStarted-AdSense
After reconnecting, we followed the configuration procedure, we pressed the Manage Ads button, the website was analyzed, we got the confirmation message and we clicked on the Save & Activate button to store the changes.

For a few hours, no ads were shown on the site. We did not take any action and on the next day that ads started appearing as expected.


Java: Create a lookup table for an Enum 4

Simple Enum

Assuming the user has a simple enum that does not have any member variable as follows:

public enum Grades {
    A, B, C, D, E, F
}

In this case, we do not need to create a map to get an enum value from an input string. We can use the enum build-in method valueOf which returns the enum constant of the specified enum type with the specified name.

Please note that the input string must match exactly an identifier used to declare an enum constant in this type. Extraneous white-space characters or wrong letter case are not permitted.

An Enum that has a member variable

Switching to a more interesting example, we assume the user has an enum type that each enum constant has a member variable. In the following example each constant has an int value as member.

public enum Weekday {

    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3),
    THURSDAY(4),
    FRIDAY(5),
    SATURDAY(6),
    SUNDAY(7);

    public int getCode() {

        return mCode;
    }

    Weekday(final int code) {

        mCode = code;
    }

    private final int mCode;
}

In this example, getting the matching enum constant for an input integer number is not as straight forward as before. We could iterate over all constants check their code and return the one that matches but that would not be efficient, especially for enum types that have many constants.

What we propose is to create a static map of elements that will have on the key part the value of the enum constant and on the value part the actual enum constant.

public enum Weekday {

    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3),
    THURSDAY(4),
    FRIDAY(5),
    SATURDAY(6),
    SUNDAY(7);

    public int getCode() {

        return mCode;
    }

    public static Weekday fromCode(final int code) {

        final Weekday element = sMap.get(code);
        if (element != null) {

            return element;
        }
        throw new IllegalArgumentException(String.format("Unknown code: <%d> for %s", code, Weekday.class.toString()));
    }

    Weekday(final int code) {

        mCode = code;
    }

    private static final Map<Integer, Weekday> sMap = new HashMap<>();

    static {
        for (final Weekday d : Weekday.values()) {

            sMap.put(d.getCode(), d);
        }
    }
    private final int mCode;
}

What we did in the above code, was to statically create the map we described. Which is, a map from the value that each enum constant holds to the constant. Then using the fromCode method we defined, we check the map for the input value, if the value is not part of the keys set of the map, it will throw an Illegal Argument Exception.

Doing Even better

An improvement we could do in the above code, would be to extract the part of the code that iterates over the fields and creates the map to reuse it. To do that, we need to use Functional Interface. Meaning we will create a function that takes as input another function that expects some input (if any) and produces as output an element of the type of the key.

Our function that can be used for all enums that their constants hold an int value is the following:

//ToIntFunction represents a function that produces an int-valued result. This is the int-producing primitive specialization for Function. This is a functional interface whose functional method is applyAsInt(Object).
public static <E extends Enum<E>> Map<Integer, E> createMap(final ToIntFunction<E> converter, final Class<E> enumClass) {

    final Map<Integer, E> map = new HashMap<>();
    for (final E s : enumClass.getEnumConstants()) {

        map.put(converter.applyAsInt(s), s);
    }
    return map;
}

The code of the enum could change to the following:

public enum Weekday {

    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3),
    THURSDAY(4),
    FRIDAY(5),
    SATURDAY(6),
    SUNDAY(7);

    public int getCode() {

        return mCode;
    }

    public static Weekday fromCode(final int code) {

        final Weekday element = sMap.get(code);
        if (element != null) {

            return element;
        }
        throw new IllegalArgumentException(String.format("Unknown code: <%d> for %s", code, Weekday.class.toString()));
    }

    Weekday(final int code) {

        mCode = code;
    }

    private static final Map<Integer, Weekday> sMap = Collections.unmodifiableMap(createMap(Weekday::getCode, Weekday.class));

    private final int mCode;
}

The function that we used as input to the new function is the getCode we defined in the enum. As you can see, it is a function that produces an int-valued result just as the requirement of the ToIntFunction parameter asks for.


Java: Breakdown a long number to the powers of two it is composed from 1

The following function will accept a long number as input and will produce an LongStream, which is a sequence of primitive long-valued elements that supports sequential and parallel aggregate operations.

public static LongStream splitToPowersOfTwo(final long input) {

    long temp = input;
    final LongStream.Builder builder = LongStream.builder();

    while (temp != 0) {

        //Long.lowestOneBit: Finds the lowest-order ("rightmost") one-bit in the specified long value and returns a long number that has only the bit in the previously matched position set as 1. This value is a power of two that is one of the components of the number. If the number is zero, the function returns zero.
        final long powerOfTwo = Long.lowestOneBit(temp);
        builder.add(powerOfTwo);
        //We update our temp by subtracting the number we got, our goal is to remove the previously matched bit and get the next one on the next iteration.
        temp = temp & ~ powerOfTwo;
    }

    return builder.build();
}

We used Long.lowestOneBit as it makes it easy for us to breakdown the number to its power of two components by matching for us only one bit at time.

Example of usage:

Scenario: We want to breakdown a long number to the powers of two that their sum produces the number and create a String with those values. Using our function, we can do the following:

final String output = splitToPowersOfTwo(flags).mapToObj(Long::toString).collect(Collectors.joining(", "));

Note: an LongStream is not the same as a Stream<Long>, we used .mapToObj(Long::toString) which calls the static method Long.toString(long). This is different to calling .map(Long::toString) on a Stream<Long> as the latter won’t compile because it is ambiguous.