Archive

Posts Tagged ‘functions’

Passing arguments to a TimerTask in Java

January 17, 2014 No comments

I recently talked about using TimerTask in Java. Today we will pass variables or attributes to that TimerTask. We saw in the last article variable passing to an implicit or inner class, but now we’re creating a subclass and passing arguments through the constructor.

MyTimerTask.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.TimerTask;

class MyTimerTask extends TimerTask
{
    private int times;
    private String result;
    private int tic=0;

    public MyTimerTask (int times, String arg2)
    {
    this.times=times;
    this.result=arg2;
    }

    public String toString()
    {
    return result;
    }

    public void run()
    {
    System.out.println((tic++%2==0)?"TIC":"TOC");
    if (tic%times==0)
        result+="TEN! ";
    }
}

TimerEx.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Timer;
import java.util.TimerTask;

class TimerEx {
    public static void main(String arglist[]) {
    long lonlon=0;
    Timer timer;
    timer = new Timer();

    TimerTask task = new MyTimerTask(10, "Start: ");

    timer.schedule(task, 10, 1000);
    try
        {
        Thread.sleep(11000);
        }
    catch (Exception e)
        {
        }
    System.out.println(task);
    }
}

In this example, we’ve passed two arguments: 10, “Start: “, to the TimerTask (MyTimerTask) constructor, they will be the number of times the task must be launched before writing “TEN! ” on a String. On the other hand, we’ve implemented the method toString in MyTimerTask to write the value directly with System.out.println().

Easy, but, what about passing a callback to execute the System.out.println() ¿?

Let’s write an interface for the Callback (MyTimerCallback), then we will implement MyTimerTask and then we will make TimerEx implement MyTimerCallback, so we can put this function in the main class:

MyTimerCallback.java

1
2
3
4
interface MyTimerCallback
{
    public void timerCallback(MyTimerTask t);
}

MyTimerTask.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.TimerTask;

class MyTimerTask extends TimerTask
{
    private int times;
    private String result="";
    private int tic=0;
    private MyTimerCallback cbClass;

    public MyTimerTask (int times, MyTimerCallback cb)
    {
    this.times=times;
    this.cbClass=cb;
    }

    public String toString()
    {
    return result;
    }

    public void run()
    {
    System.out.println((tic++%2==0)?"TIC":"TOC");
    if (tic%times==0)
        {
        result+="TEN! ";
        cbClass.timerCallback(this);
        }
    }
}

Here, in the constructor, we are asking an object type MyTimerCallback, from which we will call timerCallback().

TimerEx.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Timer;
import java.util.TimerTask;

class TimerEx implements MyTimerCallback
{
    public static void main(String arglist[])
    {
    long lonlon=0;
    Timer timer;
    timer = new Timer();

    TimerTask task = new MyTimerTask(10, new TimerEx());

    timer.schedule(task, 10, 1000);
    }

    public void timerCallback(MyTimerTask t)
    {
    System.out.println(t);
    }
}

From TimerEx, we pass a new TimerEx (as the main method is static and the class is not instanced, we must pass a new object).

Now, everytime 10 tics are executed, we will launch timerCallback(). And of course, we can do it as inner class (not instancing TimerEx, as we can see in the last example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Timer;
import java.util.TimerTask;

class TimerEx implements MyTimerCallback
{
    public static void main(String arglist[])
    {
    long lonlon=0;
    Timer timer;
    timer = new Timer();

    TimerTask task = new MyTimerTask(10, new MyTimerCallback()
        {
        public void timerCallback(MyTimerTask t)
        {
            System.out.println("Inner class: "+t);
        }
        });

    timer.schedule(task, 10, 100);
    }

    public void timerCallback(MyTimerTask t)
    {
    System.out.println(t);
    }
}

[/cc]
Foto: Rob & Stephanie Levy (Flickr) CC-by

Logging to debug MySQL

October 30, 2013 No comments

It’s common, when using more or less complex stored procedures, to make mistakes that make us waste much time, and sometimes we would like to see the value of a variable or the result of a query because it could tell us what’s going wrong.

This is a little help, an easy piece of code we can insert into our database and will let is write in a table what’s happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
DROP SCHEMA IF EXISTS `PBUtils` ;
CREATE SCHEMA IF NOT EXISTS `PBUtils` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

USE PBUtils;

DROP TABLE IF EXISTS `PBUtils`.`LOG`;
CREATE TABLE IF NOT EXISTS `PBUtils`.`LOG` (
   `Log_id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
   `Log_date` DATETIME NOT NULL,
   `Log_description` varchar(128) NOT NULL,
   `Log_text` TEXT NULL,
   `Log_number` bigint NULL,
   `Log_float` double NULL,
   PRIMARY KEY(Log_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DELIMITER $$
DROP PROCEDURE IF EXISTS `PBUtils`.`logText` $$
CREATE PROCEDURE `PBUtils`.`logText`(
    IN  In_description VARCHAR(128),
    IN  In_text TEXT
)
BEGIN
    INSERT INTO PBUtils.LOG (`Log_date`, `Log_description`, `Log_text`) VALUES (NOW(), In_description, In_text);
END $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `PBUtils`.`logNumber` $$
CREATE PROCEDURE `PBUtils`.`logNumber`(
    IN  In_description VARCHAR(128),
    IN  In_number bigint
)
BEGIN
    INSERT INTO PBUtils.LOG (`Log_date`, `Log_description`, `Log_number`) VALUES (NOW(), In_description, In_number);
END $$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `PBUtils`.`logFloat` $$
CREATE PROCEDURE `PBUtils`.`logFloat`(
    IN  In_description VARCHAR(128),
    IN  In_float double
)
BEGIN
    INSERT INTO PBUtils.LOG (`Log_date`, `Log_description`, `Log_float`) VALUES (NOW(), In_description, In_float);
END $$
DELIMITER ;

Now, what we have to do is insert our procedure to debug values, anywhere in our code:

1
PBUtils.logText('Text to describe', variable);
1
PBUtils.logNumber('Text to describe', variable);
1
PBUtils.logFloat('Text to describe', variable);

depending on what we want to record, a text, number or float

To see what has been written in our log:

1
SELECT * FROM PBUtils.LOG ORDER BY Log_data DESC

Swapping variable values in PHP [Short post]

October 11, 2013 No comments

Yesterday we talked about swapping variables in C and C++. This time, we will do it in PHP.

We can translate the C program to PHP, creating a temporary value and running the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$a = 11;
$b = 99;

echo "Original A value: ".$a."\n";
echo "Original B value: ".$b."\n";

$tmp = $a;
$a = $b;
$b = $tmp;

echo "New A value: ".$a."\n";
echo "New B value: ".$b."\n";
?>

It will simply work, but we can take advantages of built-in PHP functions, to make it shorter (and we won’t use a temporary variable):

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$a = 11;
$b = 99;

echo "Original A value: ".$a."\n";
echo "Original B value: ".$b."\n";

list($a, $b) = array($b, $a);

echo "New A value: ".$a."\n";
echo "New B value: ".$b."\n";
?>

So first we create an array with values $b and $a and then we separate this values in variables $a and $b. The result is the same.

Top