-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathISourceLocationWatcher.java
More file actions
114 lines (94 loc) · 3.48 KB
/
ISourceLocationWatcher.java
File metadata and controls
114 lines (94 loc) · 3.48 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
* Copyright (c) 2009-2021 NWO-I CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
*******************************************************************************/
package org.rascalmpl.uri;
import java.io.IOException;
import java.util.function.Consumer;
import io.usethesource.vallang.ISourceLocation;
public interface ISourceLocationWatcher {
/**
* Declares the scheme that this watcher governs
*/
String scheme();
/**
* Register a watcher callback for the current scheme at the given root
* @param root the resource to watch
* @param watcher the callback to call when something happens to the registred resource
* @param recursive also watch all nested directories for changes (use {@link #supportsRecursiveWatch()} to verify it's supported)
* @throws IOException
*/
void watch(ISourceLocation root, Consumer<ISourceLocationChanged> watcher, boolean recursive) throws IOException;
/**
* Unregister a watcher callback for a specific uri (note, there can be multiple watchers per resource, this only clears this specific watcher)
* @param root the resource to unwatch
* @param watcher the specific callback to unregister
* @param recursive was the watch registered with a recursive flag (use {@link #supportsRecursiveWatch()} to verify it's supported)
* @throws IOException
*/
void unwatch(ISourceLocation root, Consumer<ISourceLocationChanged> watcher, boolean recursive) throws IOException;
/**
* Does this watcher support a recursive watch request.
*/
boolean supportsRecursiveWatch();
public interface ISourceLocationChanged {
ISourceLocation getLocation();
ISourceLocationChangeType getChangeType();
default boolean isCreated() {
return getChangeType() == ISourceLocationChangeType.CREATED;
}
default boolean isDeleted() {
return getChangeType() == ISourceLocationChangeType.DELETED;
}
default boolean isChanged() {
return getChangeType() == ISourceLocationChangeType.MODIFIED;
}
}
public enum ISourceLocationChangeType {
CREATED(2),
DELETED(3),
MODIFIED(1);
private final int value;
public int getValue() {
return value;
}
ISourceLocationChangeType(int value) {
this.value = value;
}
public static ISourceLocationChangeType fromValue(int value) {
switch (value) {
case 2: return CREATED;
case 3: return DELETED;
case 1: return MODIFIED;
default: throw new IllegalArgumentException("Unknown ISourceLocationChangeType value " + value);
}
}
}
static ISourceLocationChanged created(ISourceLocation loc) {
return makeChange(loc, ISourceLocationChangeType.CREATED);
}
static ISourceLocationChanged deleted(ISourceLocation loc) {
return makeChange(loc, ISourceLocationChangeType.DELETED);
}
static ISourceLocationChanged modified(ISourceLocation loc) {
return makeChange(loc, ISourceLocationChangeType.MODIFIED);
}
static ISourceLocationChanged makeChange(final ISourceLocation loc, final ISourceLocationChangeType changeType) {
return new ISourceLocationChanged() {
@Override
public ISourceLocationChangeType getChangeType() {
return changeType;
}
@Override
public ISourceLocation getLocation() {
return loc;
}
};
}
}