comparison src/org/tmatesoft/hg/internal/FileChangeMonitor.java @ 610:5c68567b3645

Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 09 May 2013 21:06:48 +0200
parents
children
comparison
equal deleted inserted replaced
609:e4a71afd3c71 610:5c68567b3645
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.io.File;
20
21 /**
22 * This shall become interface/abstract class accessible from SessionContext,
23 * with plugable implementations, e.g. Java7 (file monitoring facilities) based,
24 * or any other convenient means. It shall allow both "check at the moment asked"
25 * and "collect changes and dispatch on demand" implementation approaches, so that
26 * implementors may use best available technology
27 *
28 * @author Artem Tikhomirov
29 * @author TMate Software Ltd.
30 */
31 public class FileChangeMonitor {
32 private final File file;
33 private long lastModified;
34 private long length;
35
36 /**
37 * First round: support for 1-monitor-1-file only
38 * Next round: 1-monitor-N files
39 */
40 public FileChangeMonitor(File f) {
41 file = f;
42 }
43
44 // shall work for files that do not exist
45 public void touch(Object source) {
46 lastModified = file.lastModified();
47 length = file.length();
48 }
49
50 public void check(Object source, Action onChange) {
51 if (changed(source)) {
52 onChange.changed();
53 }
54 }
55
56 public boolean changed(Object source) {
57 if (file.lastModified() != lastModified) {
58 return true;
59 }
60 return file.length() != length;
61 }
62
63 public interface Action {
64 public void changed();
65 }
66 }