Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Space Cadets
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jm15g21
Space Cadets
Commits
ed537859
Commit
ed537859
authored
3 years ago
by
jm15g21
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
544d600a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mission 2/Managers/ScopeManager.java
+58
-0
58 additions, 0 deletions
mission 2/Managers/ScopeManager.java
with
58 additions
and
0 deletions
mission 2/Managers/ScopeManager.java
0 → 100644
+
58
−
0
View file @
ed537859
package
Managers
;
import
java.util.Hashtable
;
import
java.util.Stack
;
public
class
ScopeManager
implements
IManager
{
public
Stack
<
Hashtable
<
String
,
Integer
>>
getScopedVariables
()
{
return
scopedVariables
;
}
//This method works until we introduce classes down the line
//At that point classes will need their own scope manager somehow
private
Stack
<
Hashtable
<
String
,
Integer
>>
scopedVariables
=
new
Stack
<>();
public
void
enterLocalScope
()
{
scopedVariables
.
push
(
new
Hashtable
<>());
System
.
out
.
println
(
"Entered local scope"
);
}
public
void
leaveLocalScope
()
{
scopedVariables
.
pop
();
System
.
out
.
println
(
"Left local scope"
);
}
/**
* Gets all currently accessible variables.
*/
public
Hashtable
<
String
,
Integer
>
getAllVariables
()
{
Hashtable
<
String
,
Integer
>
output
=
new
Hashtable
<>();
for
(
Hashtable
<
String
,
Integer
>
scopeLevel
:
scopedVariables
)
{
output
.
putAll
(
scopeLevel
);
}
return
output
;
}
public
void
modifyVariable
(
String
varName
,
int
newValue
)
{
//Locate the variable, if it exists modify it otherwise create it in the current scope
for
(
Hashtable
<
String
,
Integer
>
scopeLevel
:
scopedVariables
)
{
//It exists
if
(
scopeLevel
.
containsKey
(
varName
))
{
scopeLevel
.
replace
(
varName
,
newValue
);
return
;
}
}
//It doesn't exist
scopedVariables
.
peek
().
put
(
varName
,
newValue
);
}
public
void
removeVariable
(
String
varName
)
{
for
(
Hashtable
<
String
,
Integer
>
scopeLevel
:
scopedVariables
)
{
if
(
scopeLevel
.
containsKey
(
varName
))
{
scopeLevel
.
remove
(
varName
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment